简体   繁体   中英

how to group data in columns

this is my data frame and what am trying to achieve is that to group the experience columns something like this (0-5, 5-10, 10-20 ) in columns.

gender  education_level experience  training_hours  educatn_levl
0   Male    Graduate    9              21                        0
1   Female  Graduate    5              98                        0
2   Male    High School 0              15                        1
3   Male    Masters    11              39                        2
4   Male    Graduate    0              72                        0

and this is the unique value present in experience column

df2['experience'].unique()

array(['9', '5', '0', '11', '10', '14', '3', '20', '8', '4', '13', '2',
       '6', '7', '1', '19', '15', '16', 0, '17', '18', '12'], dtype=object)

You can use pd.cut Consider following sample example:

df
           A      B
0   0.039036  644.0
1   0.452201  579.0
2   0.123197  375.0
3   0.158165  799.0
4   0.469882  711.0
..       ...    ...
95  0.062099  379.0
96  0.329982  353.0
97  0.607003  668.0
98  0.782589  730.0
99  0.189787  325.0

bins = [x for x in range(0,int(df['B'].max()),100)]

df['interval'] = pd.cut(df['B'], bins)
df
           A      B    interval
0   0.039036  644.0  (600, 700]
1   0.452201  579.0  (500, 600]
2   0.123197  375.0  (300, 400]
3   0.158165  799.0  (700, 800]
4   0.469882  711.0  (700, 800]
..       ...    ...         ...
95  0.062099  379.0  (300, 400]
96  0.329982  353.0  (300, 400]
97  0.607003  668.0  (600, 700]
98  0.782589  730.0  (700, 800]
99  0.189787  325.0  (300, 400]
[100 rows x 3 columns]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM