0

I have many data frames containing information about airports organized by day.

  • One data frame will have 500 airports
  • another will have 498 airports of the same names minus 2
  • another will have 505 airports of the same names plus 5

etcetera.

I want to merge all the data frames

  • so that the total number of airports will be labeled in individual rows
  • the columns will be the data for each airport organized by day
  • if there is no data for an airport on a given day, I want it to say NAN

Is there any way to do this?

Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
Alex
  • 21
  • 2

1 Answers1

0
data_frames = [df1, df2, df3]

df_merged = reduce(lambda  left,right: pd.merge(left,right,on=['airports'],
                                            how='outer'), data_frames)

ref:-Python: pandas merge multiple dataframes

Example:

data={"Airport":["RPi-1",
"RPi-2",
"RPi-3",
"RPi-4"]}
data2={"Airport":["RPi-1",
"RPi-2",
"RPi-3",
"RPi-5"]}
data3={"Airport":["RPi-1",
"RPi-2",
"RPi-3",
"RPi-6"]}

import pandas as pd
df1 = pd.DataFrame(data)
df2 = pd.DataFrame(data2)
df3 = pd.DataFrame(data3)

from functools import reduce

data_frames = [df1, df2, df3]

df_merged = reduce(lambda  left,right: pd.merge(left,right,on=['Airport'],
                                            how='outer'), data_frames)

Output:

Airport
0   RPi-1
1   RPi-2
2   RPi-3
3   RPi-4
4   RPi-5
5   RPi-6

Note if you want to preserve date in your case if you have day column

df_merged = reduce(lambda  left,right: pd.merge(left,right,on=['Day'],
                                            how='outer'), data_frames)
Zesty Dragon
  • 551
  • 3
  • 18