pandas DF具有一列带有列表的列.如何用此列表的每个值重复行? [英] Pandas DF has one column with lists. How repeat rows with each value of this list?

查看:90
本文介绍了 pandas DF具有一列带有列表的列.如何用此列表的每个值重复行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的熊猫数据框:

I have a pandas dataframe like this:

    title   author              year    type  
0   t1      a1                  1980    article 
1   t2      ['a2', 'a3', 'a4']  1983    article 
2   t3      a5                  1982    article 
3   t4      a6                  1977    article 
4   t5      ['a7','a8']         2011    book 

这是一个简短的示例,原始文档更大.

This is a short example, the original is more big.

我需要一个这样的数据框:

And I need a dataframe like this:

    title   author   year   type  
0   t1      a1       1980   article
1   t2      a2       1983   article
2   t2      a3       1983   article 
3   t2      a4       1983   article 
4   t3      a5       1982   article 
5   t4      a6       1977   article 
6   t5      a7       2011   book
7   t5      a8       2011   book 

请注意,列表具有不同数量的元素

Note that lists have different number of elements

推荐答案

#Expand the list of authors to separate rows and build a authors df
df_author = df.author.apply(pd.Series).stack().rename('author').reset_index()

#join the authors df to the original df
pd.merge(df_author,df,left_on='level_0',right_index=True, suffixes=(['','_old']))[df.columns]

Out[184]: 
  title author  year     type
0    t1     a1  1980  article
1    t2     a2  1983  article
2    t2     a3  1983  article
3    t2     a4  1983  article
4    t3     a5  1982  article
5    t4     a6  1977  article
6    t5     a7  2011  article

这篇关于 pandas DF具有一列带有列表的列.如何用此列表的每个值重复行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆