python - How to sum numbers in Dataframe based on length of keywords? -


i have sample of search engine dataframe has 2 columns: entered keyword search , number of searches of keyword. example:

 df = pd.dataframe({'keyword': ['one','one two','2','two 34 45', 'ab', 'long 1 4 ab'], 'number of searches': ['4', '9', '1', '2', '7', '1']}) 

i sum how many searches made (not count number of words) keywords consist of 1 word, 2 words, 3 words, etc. final result must like:

1 word: 13 2 words: 9 3 words: 2 4 words: 1 

any suggestions?

you can way:

first, make sure number of searches column of integer data type:

df['number of searches'] = df['number of searches'].astype(int)  (df.groupby(df.keyword.str.split().apply(len))['number of searches']    .sum()    .to_frame()    .reset_index()    .apply(lambda x: '{0[0]} words: {0[1]}'.format(x), axis=1) ) 

output:

0    1 words: 12 1     2 words: 9 2     3 words: 2 3     4 words: 1 dtype: object 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -