pandas 多列熊猫系列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20791618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multi-column pandas series
提问by postelrich
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.
我有一个字典列表,我想加载到Pandas系列中。我想这样做,所以我可以使用 reshape 来存储我的数据。我不能或系列不允许多列。
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')
回答by joris
This is not what you want:?
这不是你想要的:?
In [1]: 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'}]
In [2]: df = pd.DataFrame(data)
In [3]: df = df.set_index('date')
In [4]: df.index = df.index.to_datetime()
In [5]: df.resample('5min', how='mean')
Out[5]:
a b
2013-09-20 20:05:00 3.333333 4.666667

