I have .csv file with column name:
id name
1 sample1
2 sample3
3 sample four
4 sample.five
5 sample.six.com
I need to print result as below (ordered by number of rows descending):
chars(str_len_count) rows(id_count)
7 2
11 2
14 1
I've tried the below, but this is not really what I'm looking for:
In [106]:
df['NAME_Count'] = df['name'].str.len()
df
Out[106]:
name NAME_Count
0 sample1 7
First new column is not necessary, you can pass str.len
to groupby
and use GroupBy.size
for count:
df1 = df.groupby(df['name'].str.len().rename('chars')).size().reset_index(name='id_count')
print (df1)
chars id_count
0 7 2
1 11 2
2 14 1
If want first create new column solution is a bit changed:
df['NAME_Count'] = df['name'].str.len()
df1 = df.groupby('NAME_Count').size().reset_index(name='count')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.