简体   繁体   English

使用嵌套列表和字典解析数据框?

[英]Parsing Dataframe with nested lists and dicts?

I'm pretty new to pandas/python and currently stuck doing this parsing. 我是pandas / python的新手,目前无法进行此解析。 Parsing Dataframe with nested lists and dicts 使用嵌套列表和字典解析数据框

DF: DF:

    a       b       c             d         e        f          

0   1       2   {'county':        3         4       [{'name': 'essex,england','locality': None,'region':'harlow','subregion':None}]
               'cleveland', 
             'country': 
                'england'}       

1   4       5   {'county':        6         7       [{'name': 'hampsire,england','locality': None,'region':'alton','subregion':None}]
               'hamphsire', 
             'country': 
                'england'}

Output should be: 输出应为:

     a      b    county     country      d       e      name        locality       region         subregion 

0   1      2   cleveland    england      3       4      essex,england      None      harlow         None


1   4      5   hamphsire    england      6        7     hampsire,england   None      alton         None

What i have tried/Known: I parsed the df if only a single row present and been successful, but two rows i couldn't do it. 我尝试过的/已知的内容:如果仅存在一行并且成功,则解析df,但是两行却无法执行。 How i parsed a single row : create a function and expand it then merge into df 我如何解析单行:创建一个函数并展开它,然后合并到df中

Original dict if needed: 原始字典,如果需要:

F column: F栏:

   [{'name': 'essex, england',
'locality': None,
'region': 'harlow',
'subregion': None,

c column c栏

'location': {0: {'county': 'cleveland',
                   'country': 'england'} 

I am not sure if I understand what you mean, but df.from_dict() should solve your problem. 我不确定我是否理解您的意思,但是df.from_dict()应该可以解决您的问题。

You can find the documentation here: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.from_dict.html 您可以在此处找到文档: https : //pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.from_dict.html

try something like: 尝试类似:

data = {dict with the data}
keys = [list with the name of the columns]
my_english_dataframe = pd.DataFrame.from_dict(data, orient='index', columns=keys)

Pandas allow for some really powerful operations, you just have to get used with how it works. 熊猫允许进行一些非常强大的操作,您只需要习惯其工作原理即可。

following your logic: 遵循您的逻辑:

import pandas as pd

df = pd.DataFrame({'a':[1,4], 
                   'b':[2,5],
                   'c':[{'county':'cleveland','country':'england'},
                        {'county':'hamphsire','country':'england'}],
                   'd':[3,6], 
                   'e':[4,7],
                   'f':[{'name': 'essex,england','locality': 
None,'region':'harlow','subregion':None},
                        {'name': 'hampsire,england','locality': 
None,'region':'alton','subregion':None}]})

df1 = df['c'].apply(pd.Series)
df2 = df['f'].apply(pd.Series)
result = pd.concat([df.filter(['a', 'b']), df1,df.filter(['d', 'e']), df2] , axis= 1)

print(result)

output: 输出:

   a  b     county  country  d  e              name locality  region subregion
0  1  2  cleveland  england  3  4     essex,england     None  harlow      None
1  4  5  hamphsire  england  6  7  hampsire,england     None   alton      None

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

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