programming freak
programming freak

Reputation: 899

how to count word numbers in python using pandas

I'm Having a csv files with 2 columns named Words and Frequency and want to return the sum of number of words with frequency of 2 and less this is the code I'm trying to use but it says

TypeError: unsupported operand type(s) for +: 'int' and 'str'

df=pd.read_csv('dictionary.csv')

result=(sum(df['word'] if df['freq']<=2))
print("the number of words appearing  less than 3 times are :{}".format{reslt})

data sample

enter image description here

Upvotes: 1

Views: 86

Answers (1)

jezrael
jezrael

Reputation: 862601

If want count number of rows matching condition, so it means number of Trues of mask only use sum:

result = (df['freq']<=2).sum()

Or:

result = df['freq'].le(2).sum()

Upvotes: 4

Related Questions