2

I have the following pandas dataframe:

In:

df = pd.DataFrame({'Fruits':['this should be a pinneapple', 
                              'this should be an apple', 
                              'this should be a tomato', 'this should 3 grapes',
                             'this should be an orange',
                              'this should be an 01',
                             'this should be an 02']})

df

Out:

    Fruits
0   this should be a pinneapple
1   this should be an apple
2   this should be a tomato
3   this should 3 grapes
4   this should be an orange
5   this should be an 01
6   this should be an 02

I would like to replace all the fruits with an id (e.g. 01 to nn). For this I tried with pandas replace function:

df['Fruits'] = df['Fruits'].replace(['pinneapple', 'apple', 'tomato', 'grapes', 'orange'],\
                                                                      ['01', '02', '03', '04', '05'])

However, when I do the above assignment nothing is done to the column I am interested to tweak. Thus, how can I replace each word for a predefined number?.

2 Answers 2

3

You can use parameter regex=True in Series.replace:

df['Fruits'] = df['Fruits'].replace(['pinneapple', 'apple', 'tomato', 'grapes', 'orange'],\
                                    ['01', '02', '03', '04', '05'], regex=True)
print (df)
                 Fruits
0   this should be a 01
1  this should be an 02
2   this should be a 03
3      this should 3 04
4  this should be an 05
5  this should be an 01
6  this should be an 02

You can also use list comprehension for codes:

fruits = ['pinneapple', 'apple', 'tomato', 'grapes', 'orange']
codes = [str(i + 1).zfill(2) for i, c in enumerate(fruits)]
print (codes)
['01', '02', '03', '04', '05']

df['Fruits'] = df['Fruits'].replace(fruits,codes, regex=True)
print (df)

                 Fruits
0   this should be a 01
1  this should be an 02
2   this should be a 03
3      this should 3 04
4  this should be an 05
5  this should be an 01
6  this should be an 02
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the help jez!
1

try resetting the value using the following:

df['Fruits'] =  pd.DataFrame()

then assign the new values again

Comments

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.