如何在列数据框熊猫中解压缩字典

时间:2020-10-31 01:44:56

标签: python arrays pandas dataframe dictionary

Stackoverflow,请尽你所能, 我有像这样的数据框熊猫

Column_one \
{{'name': 'Marfon ', 'email': '', 'phone': '123454333', 'address': 'San Jose', 'estimated_date': 2019-10-01 00:00:00, 'estimated_time': {'minimum': 1000, 'maximum': 1200, 'min': 0, 'max': 0}}
{{'name': 'Joe Doe ', 'email': 'joe@gmail.com', 'phone': '987655444', 'address': 'Carolina', 'estimated_date': 2019-10-01 00:00:00, 'estimated_time': {'minimum': 1000, 'maximum': 1200, 'min': 0, 'max': 0}}

Column_two
[{'status': False, 'item_code': 'JSK', 'price': 15000, 'note': [], 'sub_total_price': 50}] 
[{'status': False, 'item_code': 'HSO', 'price': 15000, 'note': [],  'sub_total_price': 100}] 

如何创建这样的新数据框?

name    email           phone       address     item_code
Marfon                  123454333   San Jose    JSK
Joe Doe joe@gmail.com   987655444   Carolina    HSO

解决


column_one = pd.DataFrame(main_df['Column_one'].values.tolist(), index=main_df.index)
column_two = main_df['Column_two'].apply(lambda x: ', '.join(y['item_code'] for y in x))
data_con = pd.concat([column_one, column_two], axis=1)
print(data_con)

1 个答案:

答案 0 :(得分:1)

您的输入数据有些混乱。但是,如果这是您的意思,那么:

Column_one =\
[{'name': 'Marfon ', 'email': '', 'phone': '123454333', 'address': 'San Jose', 'estimated_date': '2019-10-01 00:00:00'},
{'name': 'Joe Doe ', 'email': 'joe@gmail.com', 'phone': '987655444', 'address': 'Carolina', 'estimated_date': '2019-10-01 00:00:00'}]


Column_two=\
[{'status': False, 'item_code': 'JSK', 'price': 15000, 'note': [], 'sub_total_price': 50},
{'status': False, 'item_code': 'HSO', 'price': 15000, 'note': [],  'sub_total_price': 100}] 

pd.concat([pd.DataFrame(Column_one), pd.DataFrame(Column_two)], axis=1)

输出:

name    email   phone   address estimated_date  status  item_code   price   note    sub_total_price
Marfon      123454333   San Jose    2019-10-01 00:00:00 False   JSK 15000   []  50
Joe Doe joe@gmail.com   987655444   Carolina    2019-10-01 00:00:00 False   HSO 15000   []  100