Ran Tao
Ran Tao

Reputation: 311

Calculate number of words in Python

This is my sample datasetenter image description here

I need to create a new column "number of characters". This column should indicate the number of words in the queries. I tried to use this code,

df.words = [len(df.query.split()) for sentence in df.query]

but this error returns:

AttributeError: 'Series' object has no attribute 'query'

Upvotes: 2

Views: 129

Answers (2)

jezrael
jezrael

Reputation: 862601

You can use str.split and str.len what nice working with None and missing values also:

df = pd.DataFrame({'query':['asd vbg ffd','fd vv', None, np.nan]})

df['words'] = df['query'].str.split().str.len()
print (df)
         query  words
0  asd vbg ffd    3.0
1        fd vv    2.0
2         None    NaN
3          NaN    NaN

df['words'] =df['query'].str.split().apply(len)

TypeError: object of type 'NoneType' has no len()

Upvotes: 1

Miriam Farber
Miriam Farber

Reputation: 19634

This will do the job:

df['words'] =df['query'].str.split().apply(len)

For example,

import pandas as pd
df=pd.DataFrame({'query':['asd vbg ffd','fd vv']})
df['words'] =df['query'].str.split().apply(len)

Then df is

    query       words
0   asd vbg ffd 3
1   fd vv       2

Upvotes: 2

Related Questions