I have a pandas.DataFrame
with several columns, some with continuous data others with categorical. I've been trying to first group by category and then within each category split into arrays based on a condition (namely value between two numbers)
Here is a brute force hackjob I wrote that does the job, but I was wondering if there was a more elegant way.
import pandas as pd
df = pd.DataFrame({'Category1' : [ 0.3, 3.0, 12.4, 7.4,
20.3, 15.0, 10.9, 17.4],
'Category2' : [ 0, 0, 1, 0,
1, 1, 0, 0],
'Category3' : [ 1, 2, 3, 4,
5, 6, 7, 8],
'Category4' : ['foo','bar','fizz','buzz',
'spam','nii','blah','lol'],
etc., })
group_0_5 = df['Category1']<=5.0
group_5_10 = (df['Category1']>5.0) & (df['Category1']<=10.0)
group_10_15 = (df['Category1']>10.0) & (df['Category1']<=15.0)
group_15_20 = (df['Category1']>15.0) & df['Category1']<=20.0)
group_20_25 = (df['Category1']>20.0) & (df['Category1']<=25.0)
state1 = (df['Category2']==1)
state2 = (df['Category2']==0)
count1_state1 = df.loc[group_0_5 & state1]['Category3'].count()
count2_state1 = df.loc[group_5_10 & state1]['Category3'].count()
count3_state1 = df.loc[group_10_15 & state1]['Category3'].count()
count4_state1 = df.loc[group_15_20 & state1]['Category3'].count()
count5_state1 = df.loc[group_20_25 & state1]['Category3'].count()
count1_state2 = df.loc[group_0_5 & state2]['Category3'].count()
count2_state2 = df.loc[group_5_10 & state2]['Category3'].count()
count3_state2 = df.loc[group_10_15 & state2]['Category3'].count()
count4_state2 = df.loc[group_15_20 & state2]['Category3'].count()
count5_state2 = df.loc[group_20_25 & state2]['Category3'].count()
count_array1=[count1_state1, count2_state1, count3_state1, count4_state1, count5_state1]
count_array2=[count1_state2, count2_state2, count3_state2, count4_state2, count5_state2]
print (count_array1)
print (count_array2)
Out [2]:
[nan, nan, 2, 1, 1]
[ 2, 1, 1, 1, nan]