Skip to content

Instantly share code, notes, and snippets.

@hughdbrown
Last active April 27, 2018 00:34
Show Gist options
  • Save hughdbrown/56716437b0e83546b410d30f6b0b6092 to your computer and use it in GitHub Desktop.
Save hughdbrown/56716437b0e83546b410d30f6b0b6092 to your computer and use it in GitHub Desktop.
Create a list of dictionaries keyed by series_id
import pandas as pd
df = pd.DataFrame({
'date': list(range(20)),
'value': list(reversed(range(20))),
'series_id': [1, 2, 3, 4] * 5,
})
gr = df.groupby(["series_id"])
hist = {
key: (
df.ix[value] # Select rows in original df for this key (which is series_id)
.drop('series_id', axis=1) # Remove column we grouped by
.T # Transform so that column headings are now row
.to_dict().values() # Convert values to list of dict
)
for key, value in gr.indices.items()
}
from pprint import pprint
pprint(hist)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment