Python中的 pandas.concat()函数
pandas.concat()函数执行与轴 od Pandas 对象一起执行串联操作的所有繁重工作,同时在其他轴上执行索引(如果有)的可选设置逻辑(联合或交集)。
Syntax: concat(objs, axis, join, ignore_index, keys, levels, names, verify_integrity, sort, copy)
Parameters:
- objs: Series or DataFrame objects
- axis: axis to concatenate along; default = 0
- join: way to handle indexes on other axis; default = ‘outer’
- ignore_index: if True, do not use the index values along the concatenation axis; default = False
- keys: sequence to add an identifier to the result indexes; default = None
- levels: specific levels (unique values) to use for constructing a MultiIndex; default = None
- names: names for the levels in the resulting hierarchical index; default = None
- verify_integrity: check whether the new concatenated axis contains duplicates; default = False
- sort: sort non-concatenation axis if it is not already aligned when join is ‘outer’; default = False
- copy: if False, do not copy data unnecessarily; default = True
Returns: type of objs (Series of DataFrame)
示例 1:使用默认参数串联 2 个系列。
Python3
# importing the module
import pandas as pd
# creating the Series
series1 = pd.Series([1, 2, 3])
display('series1:', series1)
series2 = pd.Series(['A', 'B', 'C'])
display('series2:', series2)
# concatenating
display('After concatenating:')
display(pd.concat([series1, series2]))
Python3
# importing the module
import pandas as pd
# creating the Series
series1 = pd.Series([1, 2, 3])
display('series1:', series1)
series2 = pd.Series(['A', 'B', 'C'])
display('series2:', series2)
# concatenating
display('After concatenating:')
display(pd.concat([series1, series2],
axis = 1))
Python3
# importing the module
import pandas as pd
# creating the DataFrames
df1 = pd.DataFrame({'A': ['A0', 'A1', 'A2', 'A3'],
'B': ['B0', 'B1', 'B2', 'B3']})
display('df1:', df1)
df2 = pd.DataFrame({'A': ['A4', 'A5', 'A6', 'A7'],
'B': ['B4', 'B5', 'B6', 'B7']})
display('df2:', df2)
# concatenating
display('After concatenating:')
display(pd.concat([df1, df2],
keys = ['key1', 'key2']))
Python3
# importing the module
import pandas as pd
# creating the DataFrames
df1 = pd.DataFrame({'A': ['A0', 'A1', 'A2', 'A3'],
'B': ['B0', 'B1', 'B2', 'B3']})
display('df1:', df1)
df2 = pd.DataFrame({'C': ['C0', 'C1', 'C2', 'C3'],
'D': ['D0', 'D1', 'D2', 'D3']})
display('df2:', df2)
# concatenating
display('After concatenating:')
display(pd.concat([df1, df2],
axis = 1))
Python3
# importing the module
import pandas as pd
# creating the DataFrames
df1 = pd.DataFrame({'A': ['A0', 'A1', 'A2', 'A3'],
'B': ['B0', 'B1', 'B2', 'B3']})
display('df1:', df1)
df2 = pd.DataFrame({'A': ['A4', 'A5', 'A6', 'A7'],
'B': ['B4', 'B5', 'B6', 'B7']})
display('df2:', df2)
# concatenating
display('After concatenating:')
display(pd.concat([df1, df2],
ignore_index = True))
Python3
# importing the module
import pandas as pd
# creating the DataFrame
df = pd.DataFrame({'A': ['A0', 'A1', 'A2', 'A3'],
'B': ['B0', 'B1', 'B2', 'B3']})
display('df:', df1)
# creating the Series
series = pd.Series([1, 2, 3, 4])
display('series:', series)
# concatenating
display('After concatenating:')
display(pd.concat([df, series],
axis = 1))
输出:
示例 2:水平连接 2 个系列,索引 = 1
蟒蛇3
# importing the module
import pandas as pd
# creating the Series
series1 = pd.Series([1, 2, 3])
display('series1:', series1)
series2 = pd.Series(['A', 'B', 'C'])
display('series2:', series2)
# concatenating
display('After concatenating:')
display(pd.concat([series1, series2],
axis = 1))
输出:
示例 3:连接 2 个数据帧并分配键。
蟒蛇3
# importing the module
import pandas as pd
# creating the DataFrames
df1 = pd.DataFrame({'A': ['A0', 'A1', 'A2', 'A3'],
'B': ['B0', 'B1', 'B2', 'B3']})
display('df1:', df1)
df2 = pd.DataFrame({'A': ['A4', 'A5', 'A6', 'A7'],
'B': ['B4', 'B5', 'B6', 'B7']})
display('df2:', df2)
# concatenating
display('After concatenating:')
display(pd.concat([df1, df2],
keys = ['key1', 'key2']))
输出:
示例 4:使用轴 = 1 水平连接 2 个数据帧。
蟒蛇3
# importing the module
import pandas as pd
# creating the DataFrames
df1 = pd.DataFrame({'A': ['A0', 'A1', 'A2', 'A3'],
'B': ['B0', 'B1', 'B2', 'B3']})
display('df1:', df1)
df2 = pd.DataFrame({'C': ['C0', 'C1', 'C2', 'C3'],
'D': ['D0', 'D1', 'D2', 'D3']})
display('df2:', df2)
# concatenating
display('After concatenating:')
display(pd.concat([df1, df2],
axis = 1))
输出:
示例 5:使用 ignore_index = True 连接 2 个 DataFrame,以便在连接的 DataFrame 中显示新的索引值。
蟒蛇3
# importing the module
import pandas as pd
# creating the DataFrames
df1 = pd.DataFrame({'A': ['A0', 'A1', 'A2', 'A3'],
'B': ['B0', 'B1', 'B2', 'B3']})
display('df1:', df1)
df2 = pd.DataFrame({'A': ['A4', 'A5', 'A6', 'A7'],
'B': ['B4', 'B5', 'B6', 'B7']})
display('df2:', df2)
# concatenating
display('After concatenating:')
display(pd.concat([df1, df2],
ignore_index = True))
输出:
示例 6:将 DataFrame 与系列串联。
蟒蛇3
# importing the module
import pandas as pd
# creating the DataFrame
df = pd.DataFrame({'A': ['A0', 'A1', 'A2', 'A3'],
'B': ['B0', 'B1', 'B2', 'B3']})
display('df:', df1)
# creating the Series
series = pd.Series([1, 2, 3, 4])
display('series:', series)
# concatenating
display('After concatenating:')
display(pd.concat([df, series],
axis = 1))
输出: