1

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.

3
  • 1
    whats your desired output? Commented Jul 31, 2019 at 19:57
  • apply(lambda row: ..., axis=1). Commented Jul 31, 2019 at 19:57
  • typo in length? df['lenght'] = ... Commented Jul 31, 2019 at 20:00

3 Answers 3

2

We do not need to use apply

df['Fruit']+ df['lenght'].map(lambda x : ' '*x)+df['Color']
Out[689]: 
0                Apple     Red
1             Mango     Yellow
2    Watermelon          Green
dtype: object

Fix your code : 1 type , 2 adding ''

df.apply(lambda row: row['Fruit']+ (' '* row['lenght'])+row['Color'],axis=1)
Sign up to request clarification or add additional context in comments.

Comments

1

If you really want to use apply:

df['Fruit_color'] = df.apply(lambda x: x['Fruit'] + ' '*x['length'] + x['Color'], axis=1)
                                                            ^^                       ^^

Be careful of quoting column names, else Python will think you're calling a variable. Also be careful of spelling your variable names right, you used both length and lenght

Comments

1

Comprehension

zip(*map(df.get, df)) is another way to do df.itertuples(index=False)

[f'{f}{" " * l}{c}' for f, c, l in zip(*map(df.get, df))]

['Apple     Red', 'Mango     Yellow', 'Watermelon          Green']

As a new column

df.assign(New=[f'{f}{" " * l}{c}' for f, c, l in zip(*map(df.get, df))])

        Fruit   Color  length                        New
0       Apple     Red       5              Apple     Red
1       Mango  Yellow       5           Mango     Yellow
2  Watermelon   Green      10  Watermelon          Green

Without the need for an intermediate step of assigning length

df.assign(New=[f'{f}{" " * len(f)}{c}' for f, c in zip(df.Fruit, df.Color)])

        Fruit   Color                        New
0       Apple     Red              Apple     Red
1       Mango  Yellow           Mango     Yellow
2  Watermelon   Green  Watermelon          Green

2 Comments

Could you care to explain this part please:*map(df.get, df)) ? I almost understand it, but I'm not quite sure what it is doing
I added a blurb about it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.