Let's say I have the following dataframe:
| index | A |
|---|---|
| 1 | 3 |
| 2 | 5 |
| 3 | 20 |
| 4 | 8 |
| 5 | 7 |
| 6 | 13 |
| 7 | 33 |
| 8 | 2 |
I want to create a new column which is based on column A and create groups depending on the value of column A.
df = pd.DataFrame([['1', 3],['2', 5],['3', 20],['4',8],['5',7],['6',13],['7',33],['8',2]], columns=['index', 'A'])
df['groups'] = df['A'].apply(lambda x: 'high' if x>15 else 'medium' if 15>=x>10 else 'low')
How could I do the same using assign?
df = df\
.assign(groups = ?)
pd.cut(df['A'], bins=(-np.inf, 10, 15, np.inf), labels=['low','med','high'])