To stack two Pandas dataframes vertically, you can use the concat()
function with the axis
parameter set to 0
(the default value). This will combine the data from the two dataframes into a new dataframe, with the rows from the first dataframe followed by the rows from the second dataframe.
Here's an example of using the concat()
function to stack two Pandas dataframes vertically:
import pandas as pd
# create a sample dataframe
df1 = pd.DataFrame({"A": [1, 2, 3],
"B": [4, 5, 6]})
# create another sample dataframe
df2 = pd.DataFrame({"A": [7, 8, 9],
"B": [10, 11, 12]})
# stack the dataframes vertically
df_stacked = pd.concat([df1, df2], axis=0)
# display the result
print(df_stacked)
This will stack the two dataframes vertically, so that the rows from the first dataframe are followed by the rows from the second dataframe.
The axis
parameter is set to 0
to specify that the dataframes should be stacked vertically (along the rows). The resulting stacked dataframe will be displayed to the console.
The output will be:
A B
0 1 4
1 2 5
2 3 6
0 7 10
1 8 11
2 9 12
The concat()
function allows you to easily stack two dataframes vertically, by combining the rows from the two dataframes into a new dataframe. This is a convenient way to combine data from multiple dataframes into a single dataframe.
Related tutorials curated for you
How to convert a Pandas Index to a List
What does Count() do in Pandas?
What is isna() in Pandas?
What does Head() do in Pandas?
How to use Timedelta in Pandas
How to use str.contains() in Pandas
How to calculate covariance in Pandas
How to fix: AttributeError module 'pandas' has no attribute 'dataframe'
What is insert() in Pandas?
How to select multiple columns in Pandas
How to get the absolute value for a column in Pandas
What is date_range() in Pandas?