pandas concat
时间: 2023-10-14 13:28:58 浏览: 39
Pandas concat is a function that is used to concatenate two or more pandas dataframes or series along a specified axis. Concatenation is a process of combining data from different sources into a single dataset. The concat function can be used to combine dataframes or series with similar or different column names and index values.
Syntax:
```
pd.concat(objs, axis=0, join='outer', ignore_index=False, keys=None, levels=None, names=None, verify_integrity=False, sort=False, copy=True)
```
Parameters:
- objs: A sequence or mapping of Series or DataFrame objects that will be concatenated.
- axis: The axis to concatenate along (0 or 1).
- join: The type of join to perform. Acceptable values are ‘inner’, ‘outer’, ‘left’, and ‘right’. The default value is ‘outer’.
- ignore_index: A boolean value indicating whether to reset the index of the concatenated DataFrame. The default value is False.
- keys: A sequence of labels to use as the new index levels. This parameter is only valid when concatenating along the columns (axis=1).
- levels: A sequence of sequences that define the new index levels. This parameter is only valid when concatenating along the columns (axis=1).
- names: A sequence of names for the new index levels. This parameter is only valid when concatenating along the columns (axis=1).
- verify_integrity: A boolean value indicating whether to check for duplicate index values. The default value is False.
- sort: A boolean value indicating whether to sort the resulting DataFrame by the column names. The default value is False.
- copy: A boolean value indicating whether to copy the data. The default value is True.
Example:
```
import pandas as pd
# create two dataframes
df1 = pd.DataFrame({'A': ['A0', 'A1', 'A2', 'A3'],
'B': ['B0', 'B1', 'B2', 'B3'],
'C': ['C0', 'C1', 'C2', 'C3'],
'D': ['D0', 'D1', 'D2', 'D3']})
df2 = pd.DataFrame({'A': ['A4', 'A5', 'A6', 'A7'],
'B': ['B4', 'B5', 'B6', 'B7'],
'C': ['C4', 'C5', 'C6', 'C7'],
'D': ['D4', 'D5', 'D6', 'D7']})
# concatenate dataframes
result = pd.concat([df1, df2])
print(result)
```
Output:
```
A B C D
0 A0 B0 C0 D0
1 A1 B1 C1 D1
2 A2 B2 C2 D2
3 A3 B3 C3 D3
0 A4 B4 C4 D4
1 A5 B5 C5 D5
2 A6 B6 C6 D6
3 A7 B7 C7 D7
```