我对编码非常陌生,想让代码计算单词的出现频率,但是我停下来了,因为第5行存在问题-导入熊猫。我不确定该如何解决该问题。我也不确定如何对代码进行防护,因此请提前抱歉。
我已经从阅读其他Stack Overflow问题中检查了我的python版本是否与我的熊猫版本兼容,很好。我正在使用Ancaonda Navigator Spyder。
txt = " remember all those walls we built remember those times"
words = txt.split()
for word in words:
print (word + " " + str(txt.count(word))
import pandas
my table = pd.DataFrame()
for word in words:
tempdf = pd.DataFrame ({"word" : [word], "frequency" : [txt.count(word)]})
my table = mytable.append(tempdf)
print(my table)
错误消息____________________________________________________________
import pandas
<
SyntaxError: invalid syntax
您需要在第4行中添加一个额外的右括号并在第5行中将导入熊猫添加为pd,因为您使用的是pd而不是pandas
您的语法错误是由于之前 )
行上print
的右括号缺少[import pandas as pd
]。该行应显示为:
print(word + " " + str(txt.count(word)))
作为语法错误的一般提示,请先检查前一行或上一个函数调用中是否缺少括号或多余的括号。
您需要:
txt = " remember all those walls we built remember those times"
words = txt.split()
for word in words:
print(word + " " + str(txt.count(word)))
import pandas as pd
mytable = pd.DataFrame()
for word in words:
tempdf = pd.DataFrame ({"word" : [word], "frequency" : [txt.count(word)]})
mytable = mytable.append(tempdf)
print(mytable)
或更佳:
import pandas as pd
txt = " remember all those walls we built remember those times"
words = txt.split()
for word in words:
print(word + " " + str(txt.count(word)) )
my_table=pd.concat([pd.DataFrame ({"word" : [word], "frequency" : [txt.count(word)]}) for word in words])
print(mytable)