如何将列表/词典转换为数据框?

时间:2018-11-29 02:44:00

标签: python json dictionary dataframe

我有一个JSON响应(下面的示例),我试图将其转换为DataFrame。我在将数据列为列(1 x 346)等时遇到了几个问题。我只需要下面列出的5列:

area_name,
date,
month,
unemployment_rate,
year

这是我的代码:

edd_ca_df = pd.DataFrame.from_dict(edd_ca, orient="index", 
                                   columns=["area_name", "month", "date", "year", "unemployment_rate"])

这是JSON响应的示例:

[[{'area_name': 'California',
   'area_type': 'State',
   'date': '1990-01-01T00:00:00.000',
   'employment': '14099700',
   'labor_force': '14953900',
   'month': 'January',
   'seasonally_adjusted_y_n': 'N',
   'status_preliminary_final': 'Final',
   'unemployment': '854200',
   'unemployment_rate': '5.7',
   'year': '1990'},

 {'area_name': 'California',
   'area_type': 'State',
   'date': '1990-02-01T00:00:00.000',
   'employment': '14206700',
   'labor_force': '15049400',
   'month': 'February',
   'seasonally_adjusted_y_n': 'N',
   'status_preliminary_final': 'Final',
   'unemployment': '842800',
   'unemployment_rate': '5.6',
   'year': '1990'},

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

由于有了字典列表,因此就像将所有数据传递到新的DataFrame并指定要保留的列一样简单:

import pandas as pd

all_data = [{'area_name': 'California',
   'area_type': 'State',
   'date': '1990-01-01T00:00:00.000',
   'employment': '14099700',
   'labor_force': '14953900',
   'month': 'January',
   'seasonally_adjusted_y_n': 'N',
   'status_preliminary_final': 'Final',
   'unemployment': '854200',
   'unemployment_rate': '5.7',
   'year': '1990'},

 {'area_name': 'California',
   'area_type': 'State',
   'date': '1990-02-01T00:00:00.000',
   'employment': '14206700',
   'labor_force': '15049400',
   'month': 'February',
   'seasonally_adjusted_y_n': 'N',
   'status_preliminary_final': 'Final',
   'unemployment': '842800',
   'unemployment_rate': '5.6',
   'year': '1990'}]

keep_columns = ['area_name','date','month','unemployment_rate','year']
df = pd.DataFrame(columns=keep_columns, data=all_data)

print(df)

输出

    area_name                     date     month unemployment_rate  year
0  California  1990-01-01T00:00:00.000   January               5.7  1990
1  California  1990-02-01T00:00:00.000  February               5.6  1990