pandas concat 函数
pandas.concat()
方法可以连接 pandas dataframe 或 series 对象。
pandas.concat()
语法
pandas.concat(objs,
axis=0,
join='outer',
ignore_index=false,
keys=none,
levels=none,
names=none,
verify_integrity=false,
sort=false,
copy=true)
参数
objs |
要连接的 pandas series 或 dataframe 对象的序列或映射。 |
join |
连接方法(inner 或 outer ) |
axis |
沿着行(axis=0 )或列(axis=1 )进行连接 |
ignore_index |
布尔型。如果为 true ,则忽略原 dataframes 的索引。 |
keys |
向结果索引添加标识符的顺序 |
levels |
用于创建 multiindex 的级别 |
names |
多重索引中的级别名称 |
verify_integrity |
布尔型。如果为 true ,则检查是否有重复。 |
sort |
布尔型。当 join 为 outer 时,如果非 concatenation 轴尚未对齐,则对其进行排序。 |
copy |
布尔型。如果为 false ,避免不必要的数据复制。 |
返回值
如果所有的 series
对象都沿 axis=0
连接,则返回一个 series
对象。如果任何要连接的对象是一个 dataframe
,或者 series
对象沿 axis=1
连接,它返回一个 dataframe
对象。
示例:使用 pandas.concat()
方法沿行轴连接 2 个 pandas series
import pandas as pd
ser_1 = pd.series([20,45,36,45])
print("series-1:")
print(ser_1,"\n")
ser_2 = pd.series([48,46,34,38])
print("series-2:")
print(ser_2,"\n")
concatenated_ser=pd.concat([ser_1,ser_2])
print("result after concatenation of ser_1 and ser_2:")
print(concatenated_ser)
输出:
series-1:
0 20
1 45
2 36
3 45
dtype: int64
series-2:
0 48
1 46
2 34
3 38
dtype: int64
result after concatenation of ser_1 and ser_2:
0 20
1 45
2 36
3 45
0 48
1 46
2 34
3 38
dtype: int64
它沿 axis=0
或按行连接 series
对象 ser_1
和 ser_2
。其中一个 series
对象的行堆叠在另一个对象之上。级联后的对象默认取父对象的 index
值。我们可以设置 ignore_index=true
来为连接对象分配新的索引值。
import pandas as pd
ser_1 = pd.series([20,45,36,45])
print("series-1:")
print(ser_1,"\n")
ser_2 = pd.series([48,46,34,38])
print("series-2:")
print(ser_2,"\n")
concatenated_ser=pd.concat([ser_1,ser_2],ignore_index=true)
print("result after concatenation of ser_1 and ser_2:")
print(concatenated_ser)
输出:
series-1:
0 20
1 45
2 36
3 45
dtype: int64
series-2:
0 48
1 46
2 34
3 38
dtype: int64
result after concatenation of ser_1 and ser_2:
0 20
1 45
2 36
3 45
4 48
5 46
6 34
7 38
dtype: int64
它将连接 series
对象,并为连接后的 series
对象分配新的索引值。
示例:使用 pandas.concat()
方法沿列轴连接 2 个 pandas 系列对象
我们在 pandas.concat()
方法中设置 axis=1
,以水平或沿列轴连接 series
对象。
import pandas as pd
ser_1 = pd.series([20,45,36,45])
print("series-1:")
print(ser_1,"\n")
ser_2 = pd.series([48,46,34,38])
print("series-2:")
print(ser_2,"\n")
concatenated_ser=pd.concat([ser_1,ser_2],axis=1)
print("result after horizontal concatenation of ser_1 and ser_2:")
print(concatenated_ser)
输出:
series-1:
0 20
1 45
2 36
3 45
dtype: int64
series-2:
0 48
1 46
2 34
3 38
dtype: int64
result after horizontal concatenation of ser_1 and ser_2:
0 1
0 20 48
1 45 46
2 36 34
3 45 38
它水平堆叠了 series
对象 ser_1
和 ser_2
。
示例:使用 pandas.concat()
方法连接 2 个 pandas dataframe 对象
import pandas as pd
df_1 = pd.dataframe({
"col-1":[1,2,3,4],
"col-2":[5,6,7,8]
})
print("dataframe-1:")
print(df_1,"\n")
df_2 = pd.dataframe({
"col-1":[10,20,30,40],
"col-2":[50,60,70,80]
})
print("dataframe-2:")
print(df_2,"\n")
concatenated_df=pd.concat([df_1,df_2],ignore_index=true)
print("result after horizontal concatenation of df_1 and df_2:")
print(concatenated_df)
输出:
dataframe-1:
col-1 col-2
0 1 5
1 2 6
2 3 7
3 4 8
dataframe-2:
col-1 col-2
0 10 50
1 20 60
2 30 70
3 40 80
result after horizontal concatenation of df_1 and df_2:
col-1 col-2
0 1 5
1 2 6
2 3 7
3 4 8
4 10 50
5 20 60
6 30 70
7 40 80
它连接了 dataframe
对象 df_1
和 df_2
。通过设置 ignore_index=true
,我们将新的索引分配给被连接的 dataframe。
示例:使用 pandas.concat()
方法将 dataframe 与一个系列对象进行连接
import pandas as pd
df = pd.dataframe({
"col-1":[1,2,3,4],
"col-2":[5,6,7,8]
})
print("dataframe object:")
print(df,"\n")
ser = pd.series([48,46,34,38])
print("series object:")
print(ser,"\n")
ser_df=pd.concat([df,ser],axis=1)
print("concatenation of ser and df:")
print(ser_df)
输出:
dataframe object:
col-1 col-2
0 1 5
1 2 6
2 3 7
3 4 8
series object:
0 48
1 46
2 34
3 38
dtype: int64
concatenation of ser and df:
col-1 col-2 0
0 1 5 48
1 2 6 46
2 3 7 34
3 4 8 38
它将 dataframe 对象 df
和 series
对象 ser
连在一起。由于我们在 pandas.concat()
方法中设置了 axis=1
,所以连接是按列进行的。
转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处
本文地址:
相关文章
pandas dataframe dataframe.shift() 函数
发布时间:2024/04/24 浏览次数:133 分类:python
-
dataframe.shift() 函数是将 dataframe 的索引按指定的周期数进行移位。
python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:82 分类:python
-
python pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
pandas read_csv()函数
发布时间:2024/04/24 浏览次数:254 分类:python
-
pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 dataframe 中。
pandas 多列合并
发布时间:2024/04/24 浏览次数:628 分类:python
-
本教程介绍了如何在 pandas 中使用 dataframe.merge()方法合并两个 dataframes。
pandas loc vs iloc
发布时间:2024/04/24 浏览次数:837 分类:python
-
本教程介绍了如何使用 python 中的 loc 和 iloc 从 pandas dataframe 中过滤数据。
在 python 中将 pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:python
-
了解如何在 python 中将 pandas 系列日期时间转换为字符串