[英]How do I drop duplicates in panda table?
我對編碼非常陌生,並且希望代碼計算單詞的頻率,但我被阻止了,因為我不確定如何刪除重復項。
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
my_table = pd.DataFrame()
for word in words:
tempdf = pd.DataFrame ({"word" : [word], "frequency" : [txt.count(word)]})
my_table = my_table.append(tempdf)
print(my_table)
你需要:
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)
或使用pd.concat
更好:
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)
請記住,您還可以update
字典,然后在最后創建 dataframe
您需要在第 4 行添加一個額外的右括號,並在第 5 行添加 import pandas as pd 因為您使用的是 pd 而不是 pandas
您的語法錯誤是由於在 import print
import pandas as pd
之前的行上缺少右括號 ( )
)。 此行應為:
print(word + " " + str(txt.count(word)))
作為語法錯誤的一般提示,首先檢查前一行或前面的 function 調用是否缺少括號或額外的括號。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.