0

I would like to create a new column in pandas based on the ranges of another column values. I have tried some things but haven't managed it yet. Let's say that i have a column 'gdp' and i want to make a new column with classifications based on some ranges of 'gdp'. Rows with gdp below 1000 should be classified as low Rows with gdp between 1000-2000 should be classified as medium and so on.


dff['class'] = dff[(dff['gdp'] <=  1000 == 'Low') & ((dff['gdp'] >= 1000 )\
&(dff['gdp'] <= 2000 ) == 'Medium') & ((dff['gdp'] >  2001 ) == 'High')]
hippocampus
  • 347
  • 4
  • 9

1 Answers1

0

This is pd.cut:

df['class'] = pd.cut(df['gdp'], [-np.inf, 1000, 2000, np.inf], labels=('Low', 'Medium', 'High'))
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74