Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas DF has one column with lists. How repeat rows with each value of this list?

Tags:

python

pandas

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

like image 660
IvanMarkus Avatar asked Oct 15 '25 18:10

IvanMarkus


1 Answers

#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
like image 192
Allen Avatar answered Oct 18 '25 06:10

Allen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!