Search code examples
pythonpandasdataframetranspose

Python transposing multiple dataframes in a list


I have a few dataframes which are similar (in terms of number of rows and columns) to the 2 dataframes listed below

0    email           factor1_final   factor2_final   factor3_final
1    [email protected]    85%             90%             50%
2    [email protected]   80%             60%             60%
3    [email protected]  50%             70%             60%
4    [email protected]    60%             65%             50% 
5    [email protected]    98%             50%             60%
0    email           item1           item2           
1    [email protected]    80%             60%             
2    [email protected]   60%             90%             
3    [email protected]  900%            40%             
4    [email protected]    70%             35%            
5    [email protected]    92%             50%

my desired output is to have multiple dataframes with the email as column header and the factor or item as rows


email     [email protected]   [email protected]   [email protected]   [email protected]   [email protected]
factor1     85%          80%             50%              60%            98%
factor2     90%          60%             70%              65%            50% 
factor3     50%          60%             60%              50%            60%

I am able to get the result by transposing each dataframe individually using this but i'd like to create a for loop as i have several dataframes to transpose

#Set index to email and transpose
df1 = df1.set_index('email').T
df1

wrote something like this but the dataframes do not get transposed. Would like to directly change the dataframes in the list of dataframes (somewhere along the lines of inplace=True). Was wondering if there is something i am missing, appreciate any form of help, thank you.

#Create a list of all the dataframes
df_list = [df1, df2, df3, df4, df5, df6]

for df in df_list:
    df = df.set_index('email').T

df1

#tried this too but does not work 
for i, df in enumerate(df_list):
    df_list[i] = df_list[i].set_index('email').T

Solution

  • For me second solution working, here is small alternative:

    df_list = [df1, df2]
    
    for i, df in enumerate(df_list):
        df_list[i] = df.set_index('email').T
    
    print (df_list[0])
    email         [email protected] [email protected] [email protected] [email protected]  \
    factor1_final          85%           80%            50%          60%   
    factor2_final          90%           60%            70%          65%   
    factor3_final          50%           60%            60%          50%   
    
    email         [email protected]  
    factor1_final          98%  
    factor2_final          50%  
    factor3_final          60%  
    
    print (df_list[1])
    email [email protected] [email protected] [email protected] [email protected] [email protected]
    item1          80%           60%           900%          70%          92%
    item2          60%           90%            40%          35%          50%
    

    Solution with create new list of DataFrames:

    dfs = [df.set_index('email').T for df in df_list]