I want to replace same items with multiple values in pandas

Question:

Here is the code:

df = pd.DataFrame({'names': ['Fenellas', 'Steve', 'Jo', 'Stephen', 'Lili'],
                   'grade': ['High', 'High', 'Low', 'High', 'Medium']})

These two methods are not working.

df = df.replace(to_replace='High', value=['H','Hi'])
df = df.replace(to_replace='High', value='H', limit=1)

Final output should be:

names     grade 
Fenellas  H
Steve     Hi
Jo        Low
Stephen   Hi
Lili      Medium 

Is there any other good solution?

Asked By: Shakib

||

Answers:

def replacing(x):
  if x=='High':
    return 'H'
  elif x=='Medium':
    return 'M'
  elif x=='Low':
    return 'L'
  else:
    return x

df['grade'] = df['grade'].apply(replacing)

You can use the apply method to change values in your df

Answered By: Der Esel

You can use a conditional replacement.

Assuming 'High' should become 'H' for the first item, 'Hi' for the rest:

df['grade'] = (df['grade']
               .replace('High', 'H') # replace 'High' with 'H'
               # keep this for the first High
               # one could use any other condition, such as name, other col…
               .where(df.groupby('grade').cumcount().eq(0),
               # else replace with 'Hi'
                      df['grade'].replace('High', 'Hi'))
              )

output:

      names   grade
0  Fenellas       H
1     Steve      Hi
2        Jo     Low
3   Stephen      Hi
4      Lili  Medium
Answered By: mozway

Since there is no explanation on why Fenella’s High is replaced with H while others get a Hi.

# Find the exact match in `names` and replace with value:

df.loc[df['names'] == 'Fenellas', 'grade'] = 'H'

# Find and replace all `High` values in grades after running the above to change Fenella's grade.

df['grades'] = df.grades.str.replace('High','Hi')

Answered By: Sid

In case your goal was to assign ‘H’ strictly to ‘Fenellas’, and the rest get replaced ‘High’ to ‘Hi’

import numpy as np

df['grade'] = np.where((df.grade == 'High') & (df.names == 'Fenellas'), # condition
                       'H', # value if condition met
                       np.where(df.grade == 'High', # nested condition for the rest
                                'Hi', # value if nested condition met
                                df.grade)) # leave the rest intact

In case you wanted to assign ‘H’ to the first and ‘Hi’ to the rest, then:

df['grade'] = ['H' if i == 0 else 'Hi' if row.grade == 'High' else row.grade for i, row in df.iterrows()]
Answered By: n.shabankin
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.