简体   繁体   English

如何将单元格中的字典列表拆分为 dataframe 中的多行?

[英]How to split a list of dictionaries within a cell to multiple lines in a dataframe?

inp = [{'Name':'Jack', 'Twitter':'twitter.com/Jack', 'Stats': [{'Year': 2018, 'Followers': '5000'}, {'Year':2019, 'Followers': '6000'}]}, {'Name':'Bill','Twitter':'twitter.com/Twitter'}]
df = pd.DataFrame(inp)

Say I have a DataFrame like so:假设我有一个 DataFrame 像这样:

| Name | Twitter             | Stats                                                                     |
|------|---------------------|---------------------------------------------------------------------------|
| Jack | twitter.com/Jack    | [{'Year': 2018, 'Followers': '5000'}, {'Year': 2019, 'Followers': '6000'}] |
| Bill | twitter.com/Twitter |                                                                           |

How can I split the stats into multiple rows and subsequently split the dictionary into multiple columns?如何将统计信息拆分为多行,然后将字典拆分为多列? Also, if there is a NaN, just ignore the row.此外,如果存在 NaN,则忽略该行。

Desired output:所需的 output:

| Name | Twitter             | Year | Followers |
|------|---------------------|------|-----------|
| Jack | twitter.com/Jack    | 2018 | 5000      |
| Jack | twitter.com/Jack    | 2019 | 6000      |
| Bill | twitter.com/Twitter |      |           |

Here is my solution for this:这是我的解决方案:

clms = ['Name', 'Twitter', 'Year', 'Followers']
new_df = pd.DataFrame(columns=clms)
for idx, row in df.iterrows():
    try:
        for elt in row.Stats:
            new_series = pd.Series([row.Name, row.Twitter, elt['Year'], elt['Followers']], index=clms)
            new_df = new_df.append(new_series, ignore_index=True)
    except TypeError:
        new_series = pd.Series([row.Name, row.Twitter, np.NaN, np.NaN], index=clms)
        new_df = new_df.append(new_series, ignore_index=True)

So basically, this might not be super performant, but it creates a new dataframe with the corresponding split values, or NaN if the is no value in the dataframe所以基本上,这可能不是超级性能,但它会创建一个新的 dataframe 具有相应的拆分值,或者如果 dataframe 中没有值,则为 NaN

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

相关问题 如何将列表 pandas DataFrame 单元格显示为多行 - How to display a list pandas DataFrame cell as multiple lines 如何使用相同的键将列表拆分为多个字典? - How to split a list in to multiple dictionaries with the same key? 如何将带有字典列表的单列拆分为 pandas 中的多列? - How to split single column with list of dictionaries into multiple columns in pandas? 如何将字典拆分为多个字典的列表,Python 中的大小均为 N - How to split a dictionary into a list of multiple dictionaries, all of size N in Python 如何将字典列表拆分为多个保持相同索引的列? - How to split a list of dictionaries into multiple columns keeping the same index? 如何将包含多个字典的列表扩展为 dataframe? - How to expand list which contains multiple dictionaries into a dataframe? 如何在 pandas dataframe 的多列中展平字典列表 - How to flatten list of dictionaries in multiple columns of pandas dataframe 如何测试列表中多个字典中的不同值和缺失键? - How to test for different values and missing keys in multiple dictionaries within a list? 如何将字典列表转换为数据框? - How to convert a list of dictionaries to a dataframe? python-将字典列表拆分为多个字典列表,而不进行分组 - python - split list of dictionaries into multiple lists of dictionaries, without grouping
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM