2

I have multiple dataframes, the following are only 2 of them:

print(df1)

Date                 A  B  C  
2019-10-01 00:00:00  2  3  1  
2019-10-01 01:00:00  5  1  6  
2019-10-01 02:00:00  8  2  4  
2019-10-01 03:00:00  3  6  5  



print(df2)

Date                 A  B  C  
2019-10-01 00:00:00  9  4  2  
2019-10-01 01:00:00  3  2  4  
2019-10-01 02:00:00  6  5  2  
2019-10-01 03:00:00  3  6  5  

All of them have same index and columns. I want to create dataframe like this:

    Date                df1  df2
A   2019-10-01 00:00:00   2    9 
    2019-10-01 01:00:00   5    3  
    2019-10-01 02:00:00   8    6
    2019-10-01 03:00:00   3    3  
B   2019-10-01 00:00:00   3    4  
    2019-10-01 01:00:00   1    2  
    2019-10-01 02:00:00   2    5  
    2019-10-01 03:00:00   6    6  
C   2019-10-01 00:00:00   1    2  
    2019-10-01 01:00:00   6    4  
    2019-10-01 02:00:00   4    2  
    2019-10-01 03:00:00   5    5

I have to apply this process to 30 dataframes(their index and columns are same), so I want to write a for loop in order to achieve this dataframe. How can I do that?

1 Answer 1

1

Reshape each DataFrame of list of DataFrames by DataFrame.set_index with DataFrame.unstack and then concat, last change columns names with lambda function:

dfs = [df1,df2]
df = (pd.concat([x.set_index('Date').unstack() for x in dfs], axis=1)
       .rename(columns=lambda x: f'df{x+1}'))
print (df)
                       df1  df2
  Date                         
A 2019-10-01 00:00:00    2    9
  2019-10-01 01:00:00    5    3
  2019-10-01 02:00:00    8    6
  2019-10-01 03:00:00    3    3
B 2019-10-01 00:00:00    3    4
  2019-10-01 01:00:00    1    2
  2019-10-01 02:00:00    2    5
  2019-10-01 03:00:00    6    6
C 2019-10-01 00:00:00    1    2
  2019-10-01 01:00:00    6    4
  2019-10-01 02:00:00    4    2
  2019-10-01 03:00:00    5    5

If want some custom columns names in final DataFrame create list with same size like length of dfs and add parameter keys:

dfs = [df1,df2]
names = ['col1','col2']

df = pd.concat([x.set_index('Date').unstack() for x in dfs], keys=names, axis=1)
print (df)
                       col1  col2
  Date                           
A 2019-10-01 00:00:00     2     9
  2019-10-01 01:00:00     5     3
  2019-10-01 02:00:00     8     6
  2019-10-01 03:00:00     3     3
B 2019-10-01 00:00:00     3     4
  2019-10-01 01:00:00     1     2
  2019-10-01 02:00:00     2     5
  2019-10-01 03:00:00     6     6
C 2019-10-01 00:00:00     1     2
  2019-10-01 01:00:00     6     4
  2019-10-01 02:00:00     4     2
  2019-10-01 03:00:00     5     5
Sign up to request clarification or add additional context in comments.

5 Comments

What if the column names are different? I mean dfs = [Buying, Selling, Amount,..] something like that.
@JuniorESE - do you think different for each DataFrame?
No, all dataframe has same columns but only their names are different. Also column names should be df 's names not col1 and col2.
@JuniorESE - Unfortunately in python is necessary specify this columns names like in second solution in list, here called names
I want to resample Date column as monthly. Missing months will be zero. How can I do that? @ jezrael

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.