Last active
April 27, 2018 00:34
-
-
Save hughdbrown/56716437b0e83546b410d30f6b0b6092 to your computer and use it in GitHub Desktop.
Create a list of dictionaries keyed by series_id
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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