1

I got a table with some columns in it and one column got values going from 0 to 400. Let's call it column x.

Now i want to group these information in the table based on the values. I want a new column "valuerange" that says in which range the value of column x is. For example the value of the column x is at a specific row 120, therefor i want in the new column "100-150".

Maybe i need to mention that the table is a large dataframe with 210k rows.

I allready tried myself but i can't get to the expected result since i'm new to python and just used to java.

Here is some code i tried:

df1 = df['valuerange'] = ['0-50' if p<=50 '51-100' elif p<=100 '101-150' elif p<=150
                            '151-200' elif p<=200 '201-250' elif p<=250 '251-300' elif p<=300
                            '301-350' elif p<=350 '351-400' elif p<=400 for p in df.x]
FloatingGoat
  • 105
  • 1
  • 7

1 Answers1

1

pandas.cut might be what you need.

import pandas as pd
import numpy as np

df = pd.DataFrame({
    'values' : np.random.randint(low=0, high=401, size=500)
})
# df.head():
    values
0   35
1   10
2   61
3   19
4   144

df['valuerange'] = pd.cut(
    df['values'],
    bins= [0,50,100,150,200,250,300,350,400],
    labels=['0-50', '51-100',
        '100-150', '151-200', '201-250', 
        '251-300', '301-350', '351-400']
)
    values  valuerange
0   35      0-50
1   10      0-50
2   61      51-100
3   19      0-50
4   144     100-150
help-ukraine-now
  • 3,850
  • 4
  • 19
  • 36