A quick question for the universe of programmers.
DATA. Data frame column consisting of names.
g['NAME']=['John', 'Michael', 'Jezus', 'Donald', 'Suzy']
DESIRED RESULT. Another, parallel data frame column consisting of number of characters in for each name in g['NAME'].
g['NAME_count'] = [4,7,5,6,4]
Thank you in advance!
We can count by using the value_counts() method. This function is used to count the values present in the entire dataframe and also count values in a particular column.
To calculate the numbers of characters we use Series. str. len(). This function returns the count of the characters in each word in a series.
You can count the number of duplicate rows by counting True in pandas. Series obtained with duplicated() . The number of True can be counted with sum() method.
You can use vectorised str.len
to achieve this:
In [106]:
df['NAME_Count'] = df['NAME'].str.len()
df
Out[106]:
NAME NAME_Count
0 John 4
1 Michael 7
2 Jezus 5
3 Donald 6
4 Suzy 4
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With