Pandas合并之Concat合并
1.Concat语法
pd.concat(objs, axis=0, join='outer', join_axes=None, ignore_index=False,keys=None, levels=None, names=None, verify_integrity=False)
参数说明
objs:series或者dataframe对象构成的序列
asix:需要合并连接的轴,0是行,1是列
join:连接的方式,inner内连接,outer外连接
其它不常用参数......
2.Concat范例
(1)Series对象
1>Series对象的合并,横向合并纵向合并
#创建两个对象
ser_1 = pd.Series(np.random.randint(0,5,5),index=np.arange(5))
print(ser_1)
ser_2 = pd.Series(np.random.randint(5,10,5),index=np.arange(5))
print(ser_2)
'''
0 1
1 0
2 4
3 0
4 4
dtype: int32
0 7
1 9
2 6
3 5
4 5
dtype: int32
'''
#axis默认为0,是横向连接,返回一个series对象
ser = pd.concat([ser_1,ser_2])
print(ser)
'''
0 1
1 0
2 4
3 0
4 4
0 7
1 9
2 6
3 5
4 5
dtype: int32
'''
2>Series对象的合并,纵向合并,使用内连接
ser_1 = pd.Series(np.random.randint(0,10,5),index=range(5))
ser_2 = pd.Series(np.random.randint(0,10,4),index=range(4))
ser_3 = pd.Series(np.random.randint(0,10,3),index=range(3))
print(ser_1)
'''
0 4
1 3
2 6
3 4
4 6
dtype: int32
'''
print(ser_2)
'''
0 8
1 9
2 9
3 5
dtype: int32
'''
print(ser_3)
'''
0 6
1 0
2 3
dtype: int32
'''
ser = pd.concat([ser_1,ser_2,ser_3],axis=1,join='inner')
print(ser)
'''
dtype: int320 1 2
0 4 8 6
1 3 9 0
2 6 9 3
'''
3>Series对象的合并,纵向合并,使用外连接
ser_1 = pd.Series(np.random.randint(0,10,5),index=range(5))
ser_2 = pd.Series(np.random.randint(0,10,4),index=range(4))
ser_3 = pd.Series(np.random.randint(0,10,3),index=range(3))
print(ser_1)
'''
0 2
1 7
2 6
3 7
4 3
dtype: int32
'''
print(ser_2)
'''
0 7
1 4
2 7
3 0
dtype: int32
'''
print(ser_3)
'''
0 3
1 3
2 5
dtype: int32
'''
ser = pd.concat([ser_1,ser_2,ser_3],axis=1,join='outer')
print(ser)
#返回一个DataFrame对象,并且取并集,对应位置没有数据自动填充Nan
'''0 1 2
0 2 7.0 3.0
1 7 4.0 3.0
2 6 7.0 5.0
3 7 0.0 NaN
4 3 NaN NaN
'''
(2)DataFrame对象
1>DataFrame对象的合并,axis=0
# 创建两个DataFrame对象
df_1 = pd.DataFrame(np.random.randint(0,10,(3,2)),index=['a','b','c'],columns=['A','B'])
df_2 = pd.DataFrame(np.random.randint(0,10,(2,2)),index=['a','b'],columns=['C','D'])
print(df_1)
'''A B
a 7 0
b 0 8
c 9 3
'''
print(df_2)
'''C D
a 7 5
b 4 0
'''
df = pd.concat([df_1,df_2],axis=0)
print(df)
'''A B C D
a 9.0 9.0 NaN NaN
b 0.0 3.0 NaN NaN
c 4.0 7.0 NaN NaN
a NaN NaN 1.0 3.0
b NaN NaN 2.0 8.0
'''
1>DataFrame对象的合并,axis=1
# 创建两个DataFrame对象
df_1 = pd.DataFrame(np.random.randint(0,10,(3,2)),index=['a','b','c'],columns=['A','B'])
df_2 = pd.DataFrame(np.random.randint(0,10,(2,2)),index=['a','b'],columns=['C','D'])
print(df_1)
'''A B
a 5 6
b 8 9
c 4 7
'''
print(df_2)
'''C D
a 5 4
b 2 0
'''
df = pd.concat([df_1,df_2],axis=1)
print(df)
'''A B C D
a 5 6 5.0 4.0
b 8 9 2.0 0.0
c 4 7 NaN NaN
'''
(3)ndarray对象
import numpy as np
import pandas as pdarr_1 = np.random.randint(0,10,(3,4))
arr_2 = np.random.randint(0,10,(3,4))
print(arr_1)
'''
[[1 8 7 9][7 5 5 3][0 7 5 7]]
'''
print(arr_2)
'''
[[8 8 2 9][5 9 5 8][3 1 6 5]]
'''
# concatenate 函数 合并的时候有轴向
arr = np.concatenate([arr_1,arr_2],axis=0)
print(arr)
'''
[[1 8 7 9][7 5 5 3][0 7 5 7][8 8 2 9][5 9 5 8][3 1 6 5]]
'''
arr = np.concatenate([arr_1,arr_2],axis=1)
print(arr)
'''
[[1 8 7 9 8 8 2 9][7 5 5 3 5 9 5 8][0 7 5 7 3 1 6 5]]
'''
Pandas合并之Concat合并相关推荐
- pandas学习之concat合并及读写CSV文件
读取CSV文件 读取中文的CSV文件中有中文,用"UTF-8"会出现乱码问题,解决: import pandas as pd import numpy as np data=pd. ...
- pandas使用pd.concat纵向合并多个dataframe实战:纵向合并(ignore_index参数)、为纵向合并的多个dataframe设置标识符指定数据来源(通过字典方式设置数据来源键)
pandas使用pd.concat纵向合并多个dataframe实战:多个dataframe的纵向合并(ignore_index参数).为纵向合并的多个dataframe设置标识符指定数据来源(通过字 ...
- pandas使用pd.concat纵向合并多个dataframe实战:多个dataframe的纵向合并、为纵向合并的多个dataframe设置标识符指定数据来源
pandas使用pd.concat纵向合并多个dataframe实战:多个dataframe的纵向合并.为纵向合并的多个dataframe设置标识符指定数据来源 目录
- pandas使用pd.concat横向合并多个dataframe实战:多个dataframe的横向表拼接(行对齐)、多个dataframe的横向表拼接(指定join参数、交集还是并集)
pandas使用pd.concat横向合并多个dataframe实战:多个dataframe的横向表拼接(行对齐).多个dataframe的横向表拼接(指定join参数.交集还是并集) 目录
- 【Python】图解Pandas数据合并:concat、join、append
公众号:尤而小屋 作者:Peter 编辑:Peter 图解pandas数据合并:concat+join+append 在上一篇文章中介绍过pandas中最为常用的一个合并函数merge的使用,本文中介 ...
- pandas数据合并:concat、join、append
公众号:尤而小屋 作者:Peter 编辑:Peter 大家好,我是Peter~ 图解pandas数据合并:concat+join+append 在上一篇文章中介绍过pandas中最为常用的一个合并函数 ...
- pandas向下合并多个excel文件,注意concat合并出现错位混乱不对齐
一.所有excel放在文件夹合并:向下合并 import pandas as pd import os def concat_excel(path,save_name):file_name_list ...
- Python - pandas DataFrame数据的合并与拼接(merge、join、concat)
目录 0 概述 1 merge方法 1.1 内连接 1.2 外连接 1.3 左连接 1.4 右连接 1.5 基于多列的连接算法 1.6 基于index的连接方法 2 join方法 2.1 index与 ...
- python对数据进行合并的函数_利用Python pandas对Excel进行合并的方法示例
前言 在网上找了很多Python处理Excel的方法和代码,都不是很尽人意,所以自己综合网上各位大佬的方法,自己进行了优化,具体的代码如下. 博主也是新手一枚,代码肯定有很多需要优化的地方,欢迎各位大 ...
最新文章
- 区分BundleVersion和BundleShortVersionString
- UVA11624 Fire!
- Windows Server 2003 Clustering 服务
- nacos 本地测试_Nacos入门
- asterisk积累命令
- CentOS 6 安装最新的 Redis 2.8 ,安装 TCMalloc
- Android开发的经典入门教材和学习路线
- Python从excel读取数据并绘图
- python hist2d_Matplotlib(hist2D)中的2D直方图是如何工作的?
- 你一定会用到的SolidWorks快捷键汇总大全
- 两代荣耀Magic历史性同框,荣耀Magic 2如何践行科技理想主义?
- 别样的风景——韦应物《滁州西涧》赏析
- android qq存储方式,android(5)(模拟QQ登录,文件存储,SD卡存储,SharedPreferences存储)...
- python中callable什么意思_Python中的callable是基于什么样的机制实现的
- android 请假单界面,请假单_表单设计_OA系统_泛普软件
- Python——创建二维列表的简易方法
- 公用计算机配置文件,配置公用和专用计算机文件访问
- 【ps-course 网页设计】抠图
- 关于Microsoft Edge浏览器新建标签页卡顿问题解决
- Rosin-Rammler液滴粒径分布