I'm new to Pandas and am working with a multi-index data set of the form (made from groupby):
Name
Year
Month
Day
DataA DataB SpeciesName SpeciesValue
A B Name1 Value1
A B Name2 Value2
A B Name3 Value3
For every group (unique Name, Year, Month, Day) only the final two columns have a distinct value the rest of the columns are identical. I want to make each group contain a single row. The row will have the SpeciesName value as the column title and the SpeciesValue value as the entry. For instance, the result of the group above should be:
Name
Year
Month
Day
DataA DataB Name1 Name2 Name3
A B Value1 Value2 Value3
How would I go about this? Iterate through the dataframe or groupby object and create a new dataframe with the structure I want or is there a better way?
df.set_index('SpeciesName').unstack('SpeciesName')