I am trying to create a new column based on the length of a string and adding whites spaces to end of the string.
data={ 'Fruit':['Apple','Mango','Watermelon'],
'Color':['Red','Yellow','Green']
}
df = pd.DataFrame(data)
df['length']=df['Fruit'].str.len()
df['Fruit_color']=df.apply(lambda row: row['Fruit']+ (' '* row[length])+row['color'])
i get the error “TypeError: string indices must be integers”?
When i change the code only to this
df['white_space']=df.apply(lambda row: (' '* row[length]))
i get KeyError: ('length', 'occurred at index Fruit')
How do i avoid these errors to get the desired result
Regards, Ren.
apply(lambda row: ..., axis=1).length?df['lenght'] = ...