I have a pandas dataframe that roughly looks like this:
df = pd.DataFrame(data, columns=["property_1", "property_2", "value"], index=my_index)
my_index property_1 property_2 value
<1, 1, 1> "A" "X" ...
<1, 1, 1> "A" "Y" ...
<1, 1, 2> "A" "X" ...
<1, 1, 4> "A" "X" ...
<1, 1, 4> "A" "Y" ...
<1, 1, 4> "B" "X" ...
<1, 1, 4> "B" "Y" ...
I'm wanting to produce a grouped bar chart like this:
This is quite complicated, but basically:
- I need to reduce
my_index
to the unique indices that have a value for each combination ofproperty_1
andproperty_2
- I need to find the unique combinations of
property_1
ANDproperty_2
, not just the unique values of each column individually! - I am trying to group them primarily by
my_index
, and then by the combination ofproperty_1
andproperty_2
I would have guessed that the way of going about this is by having a dataframe with the following layout:
my_index A-X A-Y B-X B-Y ...
<1, 1, 1> ... ... NaN NaN ...
<1, 1, 2> ... ... NaN NaN ...
And so on. Then one could drop the columns with any NaN
values in it. You could then just call df.plot.bar(...)
on that resulting dataframe.
But I am not sure how to rearrange these rows into the columns in this way. Does anyone have any ideas?
EDIT: I should note that I don't need an answer in pandas, I am just asking if there is one. If not, I can wrangle the data out myself. But maybe pandas has a nifty one-liner for making this kind of work easier.