简体   繁体   English

Python:字典到 pandas dataframe

[英]Python: Dictionary to pandas dataframe

I would like to change the dictionary to pandas dataframe.我想将字典更改为 pandas dataframe。

data = {u'Diluted Normalized EPS': [{u'date': u'2020-01-03', u'value': u'-0.446810'}, {u'date': u'2019-10-04', u'value': u'-0.765540'}, {u'date': u'2019-06-28', u'value': u
'-0.574240'}, {u'date': u'2019-03-29', u'value': u'-2.063700'}, {u'date': u'2018-12-28', u'value': u'-0.841380'}], u'Net Income Before Extra. Items': [{u'date': u'2020-01-03', u'value': u'-139.000000'}, {u'date': u'2019-10-04', u'value': u'-276.000000'}, {u'date': u'2019-06-28', u'value': u'-185.000000'}, {u'date': u'2019-03-29', u'value': u'-652.000000'}, {u'date': u'2018-12-28', u'value': u'-257.000000'}]}

I would like to convert to Pandas dataframe as below我想转换为 Pandas dataframe 如下

         date  Diluted Normalized EPS  Net Income Before Extra. Items
0  2020-01-03  -0.446810                -139.000000
1  2019-10-04  -0.765540                -276.000000
2  2019-06-28  -0.574240                -185.000000
3  2019-03-29  -2.063700                -652.000000
4  2018-12-28  -0.841380                -257.000000

pd.DataFrame(data) unable to return the desired results pd.DataFrame(data)无法返回想要的结果

Use nested dictionary comprehension with DataFrame constructor:将嵌套字典理解与DataFrame构造函数一起使用:

d = {k:{x['date']: x['value'] for x in v} for k, v in data.items()}
df = pd.DataFrame(d).rename_axis('date').reset_index()
print(df)
         date Diluted Normalized EPS Net Income Before Extra. Items
0  2020-01-03              -0.446810                    -139.000000
1  2019-10-04              -0.765540                    -276.000000
2  2019-06-28              -0.574240                    -185.000000
3  2019-03-29              -2.063700                    -652.000000
4  2018-12-28              -0.841380                    -257.000000

Run the below code:运行以下代码:

df = pd.DataFrame.from_dict(data).apply(
    lambda col: pd.DataFrame(col.tolist()).set_index('date').value)\
    .reset_index()

Steps:脚步:

  1. pd.DataFrame.from_dict(data) - creates a temporary DataFrame. pd.DataFrame.from_dict(data) - 创建一个临时的 DataFrame。 It has 2 columns (keys of your dictionary).它有 2 列(字典的键)。 Each cell contains a dictionary, containing 2 keys ( date and value ).每个单元格包含一个字典,其中包含 2 个键(日期)。

  2. pd.DataFrame(col.tolist()) - the first part of the lambda function - converts the current column into another temporary DataFrame, with 2 columns ( date and value ). pd.DataFrame(col.tolist()) - lambda function 的第一部分 - 将当前列转换为另一个临时 DataFrame 列(日期2 列)

  3. set_index('date') - changes date column into the index. set_index('date') - 将日期列更改为索引。

  4. value - takes only value column from the above DataFrame. value - 仅取上述 DataFrame 中的value列。

  5. apply(...) - application of the above lambda function converts the first temporary DataFrame into almost exactly what we want, ie 2 required columns with date as the index. apply(...) - 应用上述lambda function 将第一个临时 DataFrame 转换为几乎正是我们想要的,即 2 个所需列,日期

  6. reset_index() - converts the index into a regular column. reset_index() - 将索引转换为常规列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM