How to merge rows in Pandas DataFrame using concat()? pandas合并行

Published on Aug. 22, 2023, 12:14 p.m.

How to merge rows in Pandas DataFrame using concat()?For merging rows in Pandas, you can use the concat function. Here is an example:

import pandas as pd

# create two dataframes
df1 = pd.DataFrame({'Col1': ['A', 'B', 'C'], 'Col2': [1, 2, 3]})
df2 = pd.DataFrame({'Col1': ['D', 'E', 'F'], 'Col2': [4, 5, 6]})

# concatenate the rows
result = pd.concat([df1, df2])

# print the result
print(result)

This will output a new DataFrame with 6 rows, where the rows from df2 are appended to the rows of df1. You can use the axis parameter to control whether the concatenation is done row-wise or column-wise. If axis=0 (default), rows are merged vertically (appended) whereas axis=1 merges horizontally (side by side).

Tags: