Reputation:
I'm very new to coding, and want the code to count the frequency of the words, but I am stopped because I'm unsure of how to remove duplicates.
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)
Upvotes: 0
Views: 59
Reputation: 30920
You need:
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)
or better with 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)
keep in mind that you could also update
the dictionary and then create the dataframe at the end
Upvotes: 1
Reputation: 140
Your syntax error is due to a closing missing parentheses ()
) for print
on the line prior to import pandas as pd
. This line should read:
print(word + " " + str(txt.count(word)))
As a general tip for syntax errors, first check the preceding line or previous function call for missing or extra parentheses.
Upvotes: 0
Reputation: 608
You need to add an extra closing bracket in line 4, and also add import pandas as pd in line 5 because you are using pd instead of pandas
Upvotes: 0