To combine dataframes in Python, you can use the concat()
function or the merge()
function from the pandas
library. Here’s an example of each method:
- Using the
concat()
function:
import pandas as pd
df1 = pd.DataFrame({'A': [1, 2, 3],
'B': ['a', 'b', 'c']})
df2 = pd.DataFrame({'A': [4, 5, 6],
'B': ['d', 'e', 'f']})
combined_df = pd.concat([df1, df2], ignore_index=True)
print(combined_df)
In this example, df1
and df2
are two dataframes with the same column names (‘A’ and ‘B’). The concat()
function is used to combine the dataframes vertically, resulting in combined_df
. The ignore_index=True
parameter is used to reindex the combined dataframe.
- Using the
merge()
function:
import pandas as pd
df1 = pd.DataFrame({'A': [1, 2, 3],
'B': ['a', 'b', 'c']})
df2 = pd.DataFrame({'A': [2, 3, 4],
'C': ['x', 'y', 'z']})
combined_df = pd.merge(df1, df2, on='A')
print(combined_df)
In this example, df1
and df2
are two dataframes with a common column ‘A’. The merge()
function is used to combine the dataframes based on the common column ‘A’, resulting in combined_df
. The on='A'
parameter specifies the common column to merge on.
You can choose the method (concat()
or merge()
) based on whether you want to combine the dataframes vertically (concatenation) or based on a common column (merge). Adjust the parameters and column names according to your specific requirements.
+ There are no comments
Add yours