[英]Converting ordered dictionary into new columns in Python Pandas
In my dataframe, some of the columns are in OrderedDictionary format.在我的 dataframe 中,一些列是 OrderedDictionary 格式。 How could I convert them into new columns (without knowing which columns contain OrderedDictionary and the elements in OrderedDictionary)我如何将它们转换为新列(不知道哪些列包含 OrderedDictionary 和 OrderedDictionary 中的元素)
Example column:示例列:
inventors
[OrderedDict([('@sequence', '001'), ('@app-type', 'applicant'), ('@designation', 'us-only'), ('addressbook', OrderedDict([('last-name', 'Nahm'), ('first-name', 'Seung Hoon'), ('address', OrderedDict([('city', 'Daejeon'), ('country', 'KR')]))])), ('residence', OrderedDict([('country', 'KR')]))]), OrderedDict([('@sequence', '002'), ('@app-type', 'applicant'), ('@designation', 'us-only'), ('addressbook', OrderedDict([('last-name', 'Jang'), ('first-name', 'Hoon Sik'), ('address', OrderedDict([('city', 'Daegu'), ('country', 'KR')]))])), ('residence', OrderedDict([('country', 'KR')]))])]
I want to convert it to the following dataframe (did not write all of the columns):我想把它转换成下面的 dataframe (没有写完所有的列):
@sequence1 @app_type1 @designation1 @last_name1 @first_name1 ....
001 applicant us_only Nahm Seung Hoon
In this example, last_name and first_name is coming from another nested dictionary.在此示例中,last_name 和 first_name 来自另一个嵌套字典。 And in the data, I don't know which columns contain OrderedDictonary, for the sake of the simplicty, I just included one column from the dataset which is inventors在数据中,我不知道哪些列包含 OrderedDictonary,为了简单起见,我只包含了数据集中的一列,它是发明者
Have you considered the pandas from_dict() function?您是否考虑过 pandas from_dict() function?
# Create example dict
import collections
inventors = collections.OrderedDict()
inventors['@sequence'] = "001"
inventors['@app-type'] = "applicant"
inventors['@designation'] = "us-only"
inventors['last-name'] = "Nahm"
inventors['first-name'] = "Seung Hoon"
# Import pandas to use from_dict function
import pandas as pd
# Use from_dict() function; include orient='index' for now to avoid index error
df = pd.DataFrame.from_dict(inventors, orient='index')
# Transpose for final output
df.T
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.