The pandas concat function is used to concatenate two or more pandas objects along a particular axis. It can be used to concatenate DataFrames or Series objects.

The basic syntax for concat is:

pd.concat(objs, axis=0, join='outer', ignore_index=False)
  • objs: A sequence or mapping of pandas objects (e.g., DataFrame, Series) to concatenate.
  • axis: The axis along which the concatenation takes place. By default, it is set to 0, which means concatenation along the rows. If set to 1, it concatenates along the columns.
  • join: Specifies how to handle the overlapping column names in the concatenated result. It can take values like 'outer' (union of all columns), 'inner' (intersection of all columns), or 'ignore' (ignores overlapping columns).
  • ignore_index: If True, it resets the index of the concatenated object. By default, it keeps the original index.

Here's an example of using concat to concatenate two DataFrames along the rows:

import pandas as pd

df1 = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
df2 = pd.DataFrame({'A': [7, 8, 9], 'B': [10, 11, 12]})

result = pd.concat([df1, df2])

print(result)

Output:

   A   B
0  1   4
1  2   5
2  3   6
0  7  10
1  8  11
2  9  12

In this example, the two DataFrames df1 and df2 are concatenated along the rows, resulting in a new DataFrame result.

pandas concat

原文地址: https://www.cveoy.top/t/topic/hUkT 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录