I have one dataframe in which first coloumn is having names. Now I want to add length of that string as a second coloumn in dataframe
list = ["apple","mango","banana","orange"]
df=pd.DataFrame(list)
is the code for dataframe for example ... I want dataframe as
0 len
0 apple 5
1 mango 5
2 banana 6
3 ora 3
You can use str.len
:
import pandas as pd
list = ["apple","mango","banana","orange"]
df=pd.DataFrame(list)
df['len'] = df.iloc[:,0].str.len()
print df
0 len
0 apple 5
1 mango 5
2 banana 6
3 orange 6
Or:
list = ["apple","mango","banana","orange"]
df=pd.DataFrame(list, columns=['a'])
df['len'] = df['a'].str.len()
print df
a len
0 apple 5
1 mango 5
2 banana 6
3 orange 6
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.