Reputation: 107
I have a bin table. In which the val
column is the bin value and the flag
is the DataFrame's column name I want to apply pd.cut
.
import re, io
import pandas as pd
data_str = '''
val flag
1 a
3 a
5 a
7 a
9 a
250 b
270 b
290 b
320 b
'''.strip()
data_str = re.sub(' +', ',', data_str)
bin_table = pd.read_csv(io.StringIO(data_str), sep=',')
print(bin_table)
val flag
0 1 a
1 3 a
2 5 a
3 7 a
4 9 a
5 250 b
6 270 b
7 290 b
8 320 b
df = pd.DataFrame(zip([2,4,8],[260,300,301]), columns=['a', 'b'])
a b
0 2 260
1 4 300
2 8 301
a a_bin b b_bin
2 (1 - 3) 260 (250 - 270)
4 (3 - 5) 300 (290 - 310)
8 (7 - 9) 301 (290 - 310)
Upvotes: 0
Views: 65
Reputation: 150735
How about:
df.groupby('flag')['val'].value_counts(bins=3)
Output:
flag
a (6.333, 9.0] 2
(0.991, 3.667] 2
(3.667, 6.333] 1
b (249.929, 273.333] 2
(296.667, 320.0] 1
(273.333, 296.667] 1
Name: val, dtype: int64
Upvotes: 1