如何将 python 字典放入一个键是日期对象的 Pandas 时间序列数据帧
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15459675/
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
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 20:42:54 来源:igfitidea点击:
How to get python dictionaries into a pandas time series dataframe where key is date object
提问by Tampa
I have a python dictionaries where the key is a dateobject and the value is the timeseires.
我有一个 python 字典,其中键是日期对象,值是时间序列。
timeseries = {datetime.datetime(2013, 3, 17, 18, 19): {'t2': 400, 't1': 1000},
datetime.datetime(2013, 3, 17, 18, 20): {'t2': 300, 't1': 3000}
}
How to I get this time series into a pandas dataframe?
如何将此时间序列放入Pandas数据框中?
回答by HYRY
use DataFrame.from_dict:
使用DataFrame.from_dict:
import pandas as pd
import datetime
timeseries = {datetime.datetime(2013, 3, 17, 18, 19): {'t2': 400, 't1': 1000},
datetime.datetime(2013, 3, 17, 18, 20): {'t2': 300, 't1': 3000}
}
print pd.DataFrame.from_dict(timeseries, orient="index")
output:
输出:
t2 t1
2013-03-17 18:19:00 400 1000
2013-03-17 18:20:00 300 3000
回答by thkang
you need Series to create a DataFrame.
您需要 Series 来创建 DataFrame。
series_dict = dict((k, Series(v)) for k, v in timeseries.iteritems())
df = DataFrame(series_dict)

