Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在dataframe中使用列表创建单词云?_Python_Pandas_List_Word Cloud - Fatal编程技术网

Python 如何在dataframe中使用列表创建单词云?

Python 如何在dataframe中使用列表创建单词云?,python,pandas,list,word-cloud,Python,Pandas,List,Word Cloud,在Python3和pandas中,我有一个数据框“proposicoes”,其中有一列单词列表。该列名为“ementa_token” 我想从“ementa_token”一栏中找出一堆单词。每行都有一个单词列表: proposicoes[proposicoes['id'] == '465465']['ementa_token'].iloc[0] ['Comunica', 'Excelentíssimo', 'Senhor', 'Presidente', 'República', 'san

在Python3和pandas中,我有一个数据框“proposicoes”,其中有一列单词列表。该列名为“ementa_token”

我想从“ementa_token”一栏中找出一堆单词。每行都有一个单词列表:

proposicoes[proposicoes['id'] == '465465']['ementa_token'].iloc[0]
['Comunica',
 'Excelentíssimo',
 'Senhor',
 'Presidente',
 'República',
 'sanção',
 'projeto',
 'lei',
 'Institui',
 'Fundo',
 'Nacional',
 'Idoso',
 'autoriza',
 'deduzir',
 'imposto',
 'renda',
 'devido',
 'pessoas',
 'físicas',
 'jurídicas',
 'doações',
 'efetuadas',
 'Fundos',
 'Municipais',
 'Estaduais',
 'Nacional',
 'Idoso',
 'altera',
 'Lei',
 'nº',
 '9250',
 '26',
 'dezembro',
 '1995',
 'restitui',
 'arquivo',
 'Congresso',
 'Nacional',
 'dois',
 'autógrafos',
 'texto',
 'ora',
 'convertido',
 'Lei',
 'nº',
 '12213',
 '20',
 'janeiro',
 '2010']
我试着这样做:

from wordcloud import WordCloud
import matplotlib.pyplot as plt
%matplotlib inline

wordcloud = WordCloud(width=800, height=400).generate(proposicoes['ementa_token'])
plt.figure( figsize=(30,20) )
plt.imshow(wordcloud)
plt.axis("off")
plt.show()
我犯了这个错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-18-c072e91a9fe7> in <module>
----> 1 wordcloud = WordCloud(width=800, height=400).generate(proposicoes['ementa_token'])
      2 plt.figure( figsize=(30,20) )
      3 plt.imshow(wordcloud)
      4 plt.axis("off")
      5 plt.show()

c:\users\reinaldo\documents\code\palavras\lib\site-packages\wordcloud\wordcloud.py in generate(self, text)
    603         self
    604         """
--> 605         return self.generate_from_text(text)
    606 
    607     def _check_generated(self):

c:\users\reinaldo\documents\code\palavras\lib\site-packages\wordcloud\wordcloud.py in generate_from_text(self, text)
    584         self
    585         """
--> 586         words = self.process_text(text)
    587         self.generate_from_frequencies(words)
    588         return self

c:\users\reinaldo\documents\code\palavras\lib\site-packages\wordcloud\wordcloud.py in process_text(self, text)
    551         regexp = self.regexp if self.regexp is not None else r"\w[\w']+"
    552 
--> 553         words = re.findall(regexp, text, flags)
    554         # remove stopwords
    555         words = [word for word in words if word.lower() not in stopwords]

c:\users\reinaldo\documents\code\palavras\lib\re.py in findall(pattern, string, flags)
    221 
    222     Empty matches are included in the result."""
--> 223     return _compile(pattern, flags).findall(string)
    224 
    225 def finditer(pattern, string, flags=0):

TypeError: expected string or bytes-like object
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在里面
---->1 wordcloud=wordcloud(宽度=800,高度=400)。生成(提案['ementa_token'])
2 plt.图(figsize=(30,20))
3 plt.imshow(wordcloud)
4 plt.轴(“关闭”)
5 plt.show()
c:\users\reinaldo\documents\code\palavras\lib\site packages\wordcloud\wordcloud.py in generate(self,text)
603自我
604         """
-->605返回self.generate_from_text(文本)
606
607 def检查生成(自):
c:\users\reinaldo\documents\code\palavras\lib\site packages\wordcloud\wordcloud.py在generate\u from\u text(self,text)中
584自我
585         """
-->586字=自处理文本(文本)
587.从_频率(单词)生成_
588回归自我
c:\users\reinaldo\documents\code\palavras\lib\site packages\wordcloud\wordcloud.py in process\u text(self,text)
551 regexp=self.regexp如果self.regexp不是None-else r“\w[\w']+”
552
-->553 words=re.findall(regexp、文本、标志)
554#删除停止字
555 words=[如果word.lower()不在stopwords中,则逐字逐句]
findall中的c:\users\reinaldo\documents\code\palavras\lib\re.py(模式、字符串、标志)
221
结果中包含222个空匹配项。”“”
-->223返回编译(模式、标志).findall(字符串)
224
225 def finditer(模式、字符串、标志=0):
TypeError:应为字符串或类似字节的对象

这是否意味着代码没有读取每行列表中的单词?请问,有人知道如何读取吗?

类型错误非常清楚,WordCloud需要的是字符串而不是序列。将列中的列表合并,然后加入

wordcloud = WordCloud(width=800, height=400).generate(' '.join(proposicoes['ementa_token'].sum())
备选案文2:

data = ' '.join(np.concatenate(df.col2))
wordcloud = WordCloud(width=800, height=400).generate(' '.join(data)

非常感谢。但是我在这个命令中遇到了这个错误:TypeError:sequence item 0:expected str instance,list found我就是这样做的。首先我创建了values->values=',join(str(v)for v in propositicoes['ementa_token']),所以我用值生成了:wordcloud=wordcloud(width=800,height=400)。generate(values)我现在的做法很有效。再次感谢你,我现在就用你的建议来测试一下