Manfred92
Manfred92

Reputation: 47

How can I create a new categories in an existing dataframe?

I have a data set with a column name total of payments, which include payments from $100 to $1000, there are some payments that are $100.05, $102 or 104.05, which make it more difficult. If the row says for instance $97, then I want to create a column that have the categories below $100 or between 100 and 200.

I have coded something like this;

Code Screenshot

but the output is the following, all of the rows say Between 800 and 900;

Output Screenshot

Please support on this folks.

Upvotes: 2

Views: 2366

Answers (1)

Vaishali
Vaishali

Reputation: 38425

You can use pandas.cut() Consider this dummy df

df = pd.DataFrame({'Total_payments': [97, 110, 100.5, 370, 820, 600]})

You can add a bracket column using pd.cut()

labels = ['<100', '100-200','200-300','300-400','400-500','500-600','600-700','700-800','800-900']
df['Brackets'] = pd.cut(df['Total_payments'], \
bins = np.arange(0, df['Total_payments'].max()+100, 100), labels = labels)


    Total_payments  Brackets
0   97.0            <100
1   110.0           100-200
2   100.5           100-200
3   370.0           300-400
4   820.0           800-900
5   600.0           500-600

Edit: updated the answer with labels parameter as suggested by @Mikhail Venkov

Upvotes: 3

Related Questions