Multi-column pandas series
Question:
I have a list of dictionaries that I want to load into a pandas Series. I want to do this so I can use reshape to bucket my data. Either I can’t or Series doesn’t allow multiple columns.
data = [{'a': 1, 'b': 3, 'date': 2013-09-20 20:07:26},
{'a': 2, 'b': 6, 'date': 2013-09-20 20:07:28},
{'a': 7, 'b': 5, 'date': 2013-09-20 20:07:33}]
Currently I can do it one column at a time, like:
data_df = to_dataframe(data) # function I wrote to load into DataFrame using from_dict and date as the index
a = Series(data_df['a'])
b = Series(data_df['b'])
a5 = a.resample('5min', how='mean')
....do some join back into a dataframe
But there must be a better way. I imagine you can do something like:
dates = pd.to_datetime(pd.Series(map(lambda x: x['date'], data)))
tseries = pandas.Series(data, dates)
bucketed_series = tseries.resample('5min', how='mean')
Answers:
This is not what you want:?
data = [{'a': 1, 'b': 3, 'date': '2013-09-20 20:07:26'},
{'a': 2, 'b': 6, 'date': '2013-09-20 20:07:28'},
{'a': 7, 'b': 5, 'date': '2013-09-20 20:07:33'}]
df = pd.DataFrame(data)
df = df.set_index('date')
df.index = df.index.to_datetime()
df.resample('5min', how='mean')
Output:
a b
2013-09-20 20:05:00 3.333333 4.666667
I have a list of dictionaries that I want to load into a pandas Series. I want to do this so I can use reshape to bucket my data. Either I can’t or Series doesn’t allow multiple columns.
data = [{'a': 1, 'b': 3, 'date': 2013-09-20 20:07:26},
{'a': 2, 'b': 6, 'date': 2013-09-20 20:07:28},
{'a': 7, 'b': 5, 'date': 2013-09-20 20:07:33}]
Currently I can do it one column at a time, like:
data_df = to_dataframe(data) # function I wrote to load into DataFrame using from_dict and date as the index
a = Series(data_df['a'])
b = Series(data_df['b'])
a5 = a.resample('5min', how='mean')
....do some join back into a dataframe
But there must be a better way. I imagine you can do something like:
dates = pd.to_datetime(pd.Series(map(lambda x: x['date'], data)))
tseries = pandas.Series(data, dates)
bucketed_series = tseries.resample('5min', how='mean')
This is not what you want:?
data = [{'a': 1, 'b': 3, 'date': '2013-09-20 20:07:26'},
{'a': 2, 'b': 6, 'date': '2013-09-20 20:07:28'},
{'a': 7, 'b': 5, 'date': '2013-09-20 20:07:33'}]
df = pd.DataFrame(data)
df = df.set_index('date')
df.index = df.index.to_datetime()
df.resample('5min', how='mean')
Output:
a b
2013-09-20 20:05:00 3.333333 4.666667