Rename variable using categorical data in pandas dataframe

Viewed 535

I was wondering how to rename observations in a pandas dataframe with more columns, for example:

User Age Value Smoke? ....
21   18   0.5    1
212  30   0.2    1
211  32   0.7    0
214  64   0.2    1
212  30   0.2    1

For one column I found that this code

pd.cut(df['Age'], bins=[-np.inf, 30, np.inf], labels=['<30', '>=30'])

should work fine. But I'd like to know how to rename ages in categorical variable (<30 and >=30), in the dataframe above, having more columns, dropping duplicate values.

Output would be

User Age Value Smoke? ....
21   <30   0.5    1
212  >=30   0.2    1
211  >=30   0.7    0
214  >=30   0.2    1
1 Answers

did you try assigning the age column to the pd.cut? like this:

df['Age'] = pd.cut(df['Age'], bins=[-np.inf, 30, np.inf], labels=['<30', '>=30'])

then to drop duplicate rows you can simply use this:

df.drop_duplicates()
Related