[英]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.