Skip to content

Instantly share code, notes, and snippets.

@theredpea
Created October 31, 2019 01:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theredpea/7f844dec76463ab9cd07efbf8dbeaace to your computer and use it in GitHub Desktop.
Save theredpea/7f844dec76463ab9cd07efbf8dbeaace to your computer and use it in GitHub Desktop.
fake_data_index = pd.MultiIndex.from_product([
[
'nate',
'jess'
], pd.date_range('2019-01-01', '2019-12-31')
], names=('who', 'date'))
fake_df = pd.DataFrame({
'increasing': np.arange(len(fake_data)),
'random': np.random.rand(len(fake_data))
}, index=fake_data_index)
(fake_df
#Homework Part 2, Step 9
#https://stackoverflow.com/questions/21646710/pandas-pivot-table-using-index-data-of-dataframe
.reset_index()
.pivot(columns='who', index='date', values='random')
.loc['2019-01-01':'2019-03-31']
.plot
.line())
by_who_group = fake_df.groupby(['who'])
mean_by_who_all_cols_df = by_who_group.mean()
pd.DataFrame({
'mean_of_increase':mean_by_who_all_cols_df['increasing'],
'sum_of_random': by_who_group.sum()['random'],
'mean_of_random': mean_by_who_all_cols_df['random']
})
by_who_group = fake_df.groupby(['who'])
agg_by_who_specific_cols = by_who_group.agg({
'increasing':'mean',
'random':['mean','sum']
})
#https://stackoverflow.com/a/14508633/1175496
agg_by_who_specific_cols.columns = [''.join(_) for _ in agg_by_who_specific_cols.columns]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment