I have to combine some dataframes in Python. I’ve tried to combine them using concat operation, but I am getting NaN values because each dataframe has different row size. For example:
DATAFRAME 1: col1 0 1 DATAFRAME 2: col2 0 5 DATAFRAME 3: col3 0 7 1 8 2 9 COMBINED DATAFRAME: col1 col2 col3 0 1.0 5.0 7 1 NaN NaN 8 2 NaN NaN 9
In this example, dataframe 1 and dataframe 2 only have 1 row. However, dataframe 3 has 3 rows. When I combine these 3 dataframes, I get NaN values for columns col1 and col2 in the new dataframe. I’d like to get a dataframe where the values for col1 and col2 are always the same. In this case, the expected dataframe would look like this:
EXPECTED DATAFRAME: col1 col2 col3 0 1 5 7 1 1 5 8 2 1 5 9
Any idea? Thanks in advance
Advertisement
Answer
Use concat
and ffill
:
df = pd.concat([df1, df2, df3], axis=1).fill()