0

There are different categories for a column in the dataframe and want to merge/rename these different columns into 3 categories and this column is object data type.I have tried using below code.. i..e using map function and also tried converting the object data type into categorical but i dont see the expected result and also see a value like when i query the unique values for that column.

Code: Tried different codes to acheive the below 3 categories

Happiest, Unhappy, Neutral

df['Status'].dtype = object

df['Status'] = df['Status'].rename({'Happy':'Happiest','Smile':'Happiest','Sad':'Un happy','ok':'Neutral'},
                                   inplace=True)

Or

Converted to categorical

df2['Status'] = df2['Status'].astype('category')

df['Status'] = df['Status'].cat.rename_categories({'Happy':'Happiest','Smile':'Happiest','Sad':'Un happy','ok':'Neutral'})

Or

df['Status'] = df['Status'].map({'Happy':'Happiest','Smile':'Happiest','Sad':'Un happy','ok':'Neutral'},
                 inplace=True)

The output of all the above tried codes are nan, happiest, Neutral or completely nan.

mozway
  • 194,879
  • 13
  • 39
  • 75
Trend
  • 1
  • 1
  • 4
  • If you are using inplace=True then the Method returns None. Try to remove the inplace keyword or the assigment. If this din't fix your Problem, can you provide a smal code example? – Exciter Mar 30 '22 at 07:31
  • please provide a minimal reproducible example if removing `inplace=True` does not solve your issue – mozway Mar 30 '22 at 07:37
  • I tried without inplace=True but the output has no changes i..e no error and no change in the o/p .................df['Status'] = df['Status'].rename({'Happy':'Happiest','Smile':'Happiest','Sad':'Un happy','ok':'Neutral'}) here the o/ when i did print(df2['Status'].unique()) : i get as 'happy','Smile','Sad' .. No change in the output. – Trend Mar 30 '22 at 08:03
  • The above are the codes that i have tried – Trend Mar 30 '22 at 08:06
  • figured out, used replace function and is working now.. – Trend Mar 30 '22 at 08:09

0 Answers0