Skip to content
Advertisement

Concat pandas dataframes in Python with different row size without getting NaN values

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:

JavaScript

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:

JavaScript

Any idea? Thanks in advance

Advertisement

Answer

Use concat and ffill:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement