Python concat columns

Viewed 59

I have 3 similar tables, each showing some spread numbers for each 24 hours. I want to combine them into 1 table so I can compare the 3 tables.

So the result should have 4 columns and 25 row, while the first row and column are titles

And how to change the titles for each 3 columns after combining?

import pandas as pd

hour = ['00', '01', '02', '03', '04', '06', '07', '08', '09', '10',
'11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21',
'22', '23']

spread = [27.461988, 2.144416, 0.970719, 0.883571, 1.234078, 0.747148,
0.660058, 1.025625, 0.660939, 0.600193, 0.412775, 0.503613, 0.468141,
0.417250, 0.366429, 0.414767, 0.295326, 0.289255, 0.091598, 0.312621,
0.393910, 0.490924, 0.425078, 1.350392]

df = pd.DataFrame(list(zip(hour, spread)),columns = ['hour','spread'])
df.set_index("hour", inplace = True)
    spread
hour    
00  27.461988
01  2.144416
02  0.970719
03  0.883571
04  1.234078
06  0.747148
07  0.660058
08  1.025625
09  0.660939
10  0.600193
11  0.412775
12  0.503613
13  0.468141
14  0.417250
15  0.366429
16  0.414767
17  0.295326
18  0.289255
19  0.091598
20  0.312621
21  0.393910
22  0.490924
23  0.425078
2 Answers

Inner join on hour using pandas.

(pd.merge(
         pd.merge(df1, 
             df2, on=["hour"]), 
         df3, on=["hour"])
)

You can just name them after concat which overwrites the previous values:
(see below for a rename with the same effect)

1st generation of 3 dataframes with the same column name:

import pandas as pd

hour = ['00', '01', '02', '03', '04', '06', '07', '08', '09', '10',
'11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21',
'22', '23']

spread = [27.461988, 2.144416, 0.970719, 0.883571, 1.234078, 0.747148,
0.660058, 1.025625, 0.660939, 0.600193, 0.412775, 0.503613, 0.468141,
0.417250, 0.366429, 0.414767, 0.295326, 0.289255, 0.091598, 0.312621,
0.393910, 0.490924, 0.425078, 1.350392]

df_1 = pd.DataFrame(list(zip(hour, spread)),columns = ['hour','spread'])
df_1.set_index("hour", inplace = True)

spread = spread[3:] + spread[:3]
df_2 = pd.DataFrame(list(zip(hour, spread)),columns = ['hour','spread'])
df_2.set_index("hour", inplace = True)

spread = [ x/2 for x in spread] 
df_3 = pd.DataFrame(list(zip(hour, spread)),columns = ['hour','spread'])
df_3.set_index("hour", inplace = True)

2nd concat, note the the generic columns in the result:

df_concat = pd.concat([df_1, df_2, df_3],
                      ignore_index=True, axis=1)
df_concat.head(3)
              0         1         2
hour                               
00    27.461988  0.883571  0.441785
01     2.144416  1.234078  0.617039
02     0.970719  0.747148  0.373574

3rd (re)name the colums - that overwrites any previous names:

df_concat.columns =['spread_1', 'spread_2', 'spread_3']
df_concat.head(3)
    spread_1    spread_2    spread_3
hour            
00  27.461988   0.883571    0.441785
01  2.144416    1.234078    0.617039
02  0.970719    0.747148    0.373574

You can also use .rename with the same effect:

df_concat.rename(columns={df_concat.columns[0]: "spread_1",
                          df_concat.columns[1]: "spread_2",
                          df_concat.columns[2]: "spread_3"}, inplace = True)
Related