简体   繁体   中英

Loop to transpose and concatenate list of dataframes

I have a list of dataframes with differing no. of rows: I want to transpose each dataframe in the list and concatenate it to one dataframe. Since there are over 600 dataframes in my list, I wanted to use a loop... I was only able to apply this to single dataframes.

[     Score
 0    0.000
 1    0.050
 2    0.016
 3    0.007
 4    0.424
 ..     ...
 346  0.038

 [347 rows x 1 columns],      Score
 0    0.100
 1    4.006
 2    0.598
 3    0.005
 4    9.007
 ..     ...
 390  0.050
[391 rows x 1 columns], .... ]

Code for one single data frame:

df = list[0] 
df_transposed = df.T
df_transposed.rename(index={'Score':0}, inplace=True)
df_transposed

My try:

df_final = []
for i in list:
    df = list[i]
    df_transposed = df.T
    df_transposed.rename(index={'Score':0}, inplace=True)
    df_final.append(df_transposed)

How can I do it more efficiently for all the dataframes in my list??

First dont use variable list , because python code word ( builtin ). Change list to L and use list comprehension:

df_final = [x.T.rename(index={'Score':0}) for x in L]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM