Home > OS >  pandas expand or duplicate rows depend on specific columns
pandas expand or duplicate rows depend on specific columns

Time:08-05

First, sorry for my Poor English skills and making hard to understand question by Title.

What I am trying to do is,

I have a dataframe and a list:

 col1 col2 col3
 ---  ---  ---
 a1   b1   c1       
 a2   b2   c2      
 a3   b3   c3              

list = [v1, v2, v3, v4]

And I want to make dataframe like this:

     col1 col2 col3 col4
     ---  ---  ---  ---
     a1   b1   c1   v1   
     a1   b1   c1   v2  
     a1   b1   c1   v3  
     a1   b1   c1   v4   
     a2   b2   c2   v1    
     a2   b2   c2   v2   
     a2   b2   c2   v3   
     a2   b2   c2   v4  
     a3   b3   c3   v1 
     a3   b3   c3   v2 
     a3   b3   c3   v3 
     a3   b3   c3   v4

I am trying to do this work by using .melt functions,

Is there any better idea?

CodePudding user response:

I think you are looking for a many-to-many join case, i.e. cartesian product.

What you can do is convert your list to a pd.DataFrame, and create a key column (create the key column for your df as well). Then merge the two dataframes on the key column:

# Convert list to dataframe & create 'key' columns
li = ['v1', 'v2', 'v3', 'v4']
li_df = pd.DataFrame(li,columns=['col4']).assign(key=1)
df['key'] = 1
>>> pd.merge(li_df,df,on='key').drop('key',axis=1).sort_values(by=['col1']).sort_index(axis=1)

   col1 col2 col3 col4
0    a1   b1   c1   v1
3    a1   b1   c1   v2
6    a1   b1   c1   v3
9    a1   b1   c1   v4
1    a2   b2   c2   v1
4    a2   b2   c2   v2
7    a2   b2   c2   v3
10   a2   b2   c2   v4
2    a3   b3   c3   v1
5    a3   b3   c3   v2
8    a3   b3   c3   v3
11   a3   b3   c3   v4
  • Related