How to combine multiple dataframe rows together with different column sizes in python pandas
In this tutorial, we will look at how to combine multiple dataframe rows together with different column sizes using the Python Pandas library.
In this tutorial, we will look at how to combine multiple dataframe rows together with different column sizes using the Python Pandas library.
First, let’s start by creating two sample dataframes.
import pandas as pd
df1 = pd.DataFrame({'A': ['A0', 'A1', 'A2'], 'B': ['B0', 'B1', 'B2'], 'C': ['C0', 'C1', 'C2'], 'D': ['D0', 'D1', 'D2']})
df2 = pd.DataFrame({'A': ['A3', 'A4', 'A5', 'A6'], 'B': ['B3', 'B4', 'B5', 'B6'], 'C': ['C3', 'C4', 'C5', 'C6'], 'D': ['D3', 'D4', 'D5', 'D6'], 'E': ['E3', 'E4', 'E5', 'E6']})
print(df1)print(df2)This will print out the following dataframes:
A B C D0 A0 B0 C0 D01 A1 B1 C1 D12 A2 B2 C2 D2
A B C D E0 A3 B3 C3 D3 E31 A4 B4 C4 D4 E42 A5 B5 C5 D5 E53 A6 B6 C6 D6 E6Now, let’s say we want to combine these two dataframes together such that they are stacked on top of each other, with the rows of df2 coming after the rows of df1. In other words, the final dataframe should look like this:
A B C D E0 A0 B0 C0 D0 NaN1 A1 B1 C1 D1 NaN2 A2 B2 C2 D2 NaN3 A3 B3 C3 D3 E34 A4 B4 C4 D4 E45 A5 B5 C5 D5 E56 A6 B6 C6 D6 E6To do this, we can use the pd.concat() function. This function can be used to concatenate dataframes along either the rows or columns. We want to concatenate along the rows, so we set the axis parameter to 0.
df_combined = pd.concat([df1, df2], axis=0)print(df_combined)This will print out the combined dataframe as shown above. Notice that the resulting dataframe has NaN values for the missing values in the E column of df1.
Alternatively, we can also use the df1.append() method to achieve the same result. This method can be used to append rows of one dataframe to the end of another dataframe.
df_combined = df1.append(df2)print(df_combined)This will also print out the combined dataframe as shown above.
Note that both the pd.concat() and df.append() functions will align the dataframes based on their columns. If the dataframes have different column names, the resulting dataframe will have NaN values for the missing values.
If you want to specify which columns should be used to align the dataframes, you can use the join parameter of the pd.concat() function. For example:
df_combined = pd.concat([df1, df2], axis=0, join='inner')print(df_combined)This will only include the columns that are present in both df1 and df2, and will print out the following dataframe:
A B C D0 A0 B0 C0 D01 A1 B1 C1 D12 A2 B2 C2 D23 A3 B3 C3 D34 A4 B4 C4 D45 A5 B5 C5 D56 A6 B6 C6 D6I hope this tutorial helps! Let me know if you have any questions.
Comments