Reputation: 311
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
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
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