Template request | Bug report | Generate Data Product
Tags: #pandas #concatenate #dataframe #snippet #operations
Author: Benjamin Filly
Description: This notebook demonstrates how to concatenate dataframes across rows or columns using the Pandas library.
References:
import pandas as pd
df1
: DataFrame objectdf2
: DataFrame objectaxis
: The axis to concatenate along. 0 ="index", 1 ="columns"
df1 = pd.DataFrame({'A': [1, 2, 3, 4], 'B': [4, 5, 6, 7]})
df2 = pd.DataFrame({'A': [7, 8, 9], 'B': [10, 11, 12]})
axis = 0
print("Columns fetched:", len(df1.columns))
print("Rows fetched:", len(df1))
df1
print("Columns fetched:", len(df2.columns))
print("Rows fetched:", len(df2))
df2
df_concat = pd.concat([df1, df2], axis=axis).reset_index(drop=True)
print("Columns fetched:", len(df_concat.columns))
print("Row fetched:", len(df_concat))
df_concat