I have this simple dataframe df:
City,H
AMS,1.1
AMS,0.8
AMS,0.9
BOS,0.9
BOS,0.7
BOS,0.6
BOS,0.8
I would like to sort the H columns according to each City, and then plot each City index with a different color. So far I started by grouping and sorting:
d = df.groupby('City').apply(lambda x: x.sort('H', ascending=False))
Then, since I would like to plot the H values for each City according to a sort of ranking, I add a column called subindex as:
d['subindex'] = d.groupby(level=0).cumcount() + 1
The resulting dataframe is:
City H subindex
City
AMS 0 AMS 1.1 1
2 AMS 0.9 2
1 AMS 0.8 3
BOS 3 BOS 0.9 1
6 BOS 0.8 2
4 BOS 0.7 3
5 BOS 0.6 4
The format is what I wanted, but I can't figured out why the column City appears twice. Now the problem is plotting, for each City, the H values according to the subindex. I tried:
for i, group in d:
group.plot(x='subindex', y='H')
receiving the following ValueError:
for i, group in d:
ValueError: too many values to unpack


groupbybut a multi-indexed df, so you wantd.index.get_level_values(0).unique()