Coding Ref

How to concatenate in Pandas

How to concatenate in Pandas

To concatenate two or more Pandas DataFrames, you can use the .concat() method.

This method will concatenate the rows or columns of the given DataFrames, depending on the value of the axis parameter.

Examples

Here are a few examples:

main.py
import pandas as pd

# Create two sample Pandas DataFrames
df1 = pd.DataFrame({'fruit': ['apple', 'orange', 'apple', 'banana'],
                    'color': ['red', 'orange', 'green', 'yellow']})

df2 = pd.DataFrame({'fruit': ['pineapple', 'strawberry', 'banana'],
                    'color': ['yellow', 'red', 'brown']})

# Use the concat() method to concatenate the DataFrames by rows
df = pd.concat([df1, df2], axis=0)

# Print the concatenated DataFrame
print(df)

This will output the following DataFrame, which contains the rows from both of the input DataFrames:

output
        fruit   color
0       apple     red
1      orange  orange
2       apple   green
3      banana  yellow
0   pineapple  yellow
1  strawberry     red
2      banana   brown

You can also concatenate the DataFrames by columns, using the axis=1 parameter.

For example:

main.py
# Use the concat() method to concatenate the DataFrames by columns
df = pd.concat([df1, df2], axis=1)

# Print the concatenated DataFrame
print(df)

This will output the following DataFrame, which contains the columns from both of the input DataFrames:

output
    fruit   color       fruit   color
0   apple     red   pineapple  yellow
1  orange  orange  strawberry     red
2   apple   green      banana   brown
3  banana  yellow         NaN     NaN

In all of these examples, df1 and df2 are the names of the input DataFrames, and df is the name of the concatenated DataFrame.

You can replace these names with the names of your own DataFrames if they are different.

You'll also like

Related tutorials curated for you

    How to give multiple conditions in loc() in Pandas

    How to drop duplicate columns in Pandas

    How to select multiple columns in Pandas

    How to drop duplicate rows in Pandas

    How to convert a series to a NumPy array in Pandas

    How to calculate the standard deviation in Pandas DataFrame

    How to print a specific row in a Pandas DataFrame?

    How to split a Pandas DataFrame by a column value

    How to convert string to float in Pandas

    What is isna() in Pandas?

    How to filter a Pandas DataFrame

    How to use ewm() in Pandas