Create histogram from panda frame


Create histogram from panda frame



I am trying to created bar histogram that will show the mean of subjects by groups



my data looks like this -


week 8 exp
Subject Group 1 2 3 Mean
0 255 WT 0 101.8 75.6 84.1 87.166667
1 157 HD 0 92.6 87.8 82.3 87.566667
2 418 WT 0 54.5 47.0 50.8 50.766667
3 300 WT 0 48.1 73.1 72.2 64.466667
4 299 HD 0 71.8 86.0 93.4 83.733333
5 258 WT 0 88.0 98.5 50.2 78.900000
6 173 WT 0 75.4 70.5 83.9 76.600000
7 273 HD 0 103.6 94.2 108.3 102.033333
8 175 WT 0 36.7 30.7 42.2 36.533333
9 172 HD 0 82.6 91.6 73.4 82.533333
10 263 WT 0 110.7 102.4 105.5 106.200000
11 304 1 90.4 90.1 103.4 94.633333
12 305 1 128.6 141.5 123.1 131.066667
13 306 1 52.0 45.6 57.2 51.600000
14 309 0.1 41.3 52.6 79.9 57.933333
15 317 0.1 86.2 95.8 77.1 86.366667



My code is -


frame_data = pd.read_csv('final results.csv', header=[0,1])

data_avg = df.iloc[:, -3:].mean(axis=1)

frame_data[('exp', 'Mean')] = frame_data.iloc[:, -3:].mean(axis=1)

grouped_by_group = frame_data.groupby(['Group',
'Mean']).size().unstack('Mean')

grouped_by_group.plot.bar(title='Grip')



I am getting an error
KeyError: 'Group'



i checked many times and it is the way it is written... I do not know what is wrong...





Group doesn't exist in the context you have selected, which is why you get a KeyError
– user3483203
Jun 30 at 7:05


Group


KeyError




1 Answer
1



I think need reshape DataFrame by melt, aggregate mean and then then Series.plot:


DataFrame


melt


mean


Series.plot


frame_data = pd.read_csv('final results.csv', header=[0,1])
frame_data[('exp', 'Mean')] = frame_data.iloc[:, -3:].mean(axis=1)

#flatten MultiIndex to columns
frame_data.columns = frame_data.columns.map('_'.join)

grouped_by_group = frame_data.groupby('8_Group')['exp_Mean'].mean()
print (grouped_by_group)
8_Group
0.1 72.150000
1 92.433333
HD 0 88.966667
WT 0 71.519048
Name: value, dtype: float64

grouped_by_group.plot.bar(title='Grip')





while running grouped_by_group = frame_data.groupby([('12','Group'), ('exp','Mean')]).size().unstack() I am getting an error ValueError: operands could not be broadcast together with shapes (2,) (16,) (2,)
– Talya Shacham
Jun 30 at 7:23






ok after getting an heart attack from the new shape it does able to make an histogram. but, it puted every subject in it own bar and not by average of the 4 groups
– Talya Shacham
Jun 30 at 7:28






ok. now i get bars for each group for each subject. what i need is for each group one bar, that holds the average of the subject of the group... i was trying to do that by grouping but it doesnt seems to be enough
– Talya Shacham
Jun 30 at 7:35






The groups are 0 WT, 0 HD, o.1 and 1. So i need 4 bars in the graph at the end. The bar of 1 for example should be the average of the means of subjects 304, 305,306
– Talya Shacham
Jun 30 at 7:42






awsome. that did the trick. thanks!
– Talya Shacham
Jun 30 at 18:11






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

.

How to generate build files in next js

Delphi Android file open failure with API 26