博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python Pandas concat 的使用
阅读量:7023 次
发布时间:2019-06-28

本文共 1842 字,大约阅读时间需要 6 分钟。

hot3.png

1. axis(合并方向)


import pandas as pdimport numpy as npdf1 = pd.DataFrame(np.ones((3, 4)) * 0, columns = ['a', 'b', 'c', 'd'])df2 = pd.DataFrame(np.ones((3, 4)) * 1, columns = ['a', 'b', 'c', 'd'])df3 = pd.DataFrame(np.ones((3, 4)) * 2, columns = ['a', 'b', 'c', 'd'])# concat# 合并df1、df2、df3# axis=0 纵向合并,axis=1 横向合并# ignore_index=True 表示忽略以前的index,index重新生成res =pd.concat([df1, df2, df3], axis = 0, ignore_index = True)print(res)

2. join, ['inner', 'outer'] (合并方式)


import pandas as pdimport numpy as np# join, ['inner', 'outer']df1 = pd.DataFrame(np.ones((3, 4)) * 0, columns = ['a', 'b', 'c', 'd'], index = [1, 2, 3])df2 = pd.DataFrame(np.ones((3, 4)) * 1, columns = ['b', 'c', 'd', 'e'], index = [1, 2, 3])print(df1)print(df2)# join默认outer模式,会将没有数据的位置使用NaN填充,类似于字段并集res = pd.concat([df1, df2], join = 'outer')print(res)# join='inner',会将相同的部分进行合并,不同的部分被抛弃掉,类似于字段交集res2 = pd.concat([df1, df2], join = 'inner', ignore_index = True)print(res2)

3. join_axes(依照 axes 合并)


import pandas as pdimport numpy as np# join_axesdf1 = pd.DataFrame(np.ones((3, 4)) * 0, columns = ['a', 'b', 'c', 'd'], index = [1, 2, 3])df2 = pd.DataFrame(np.ones((3, 4)) * 1, columns = ['b', 'c', 'd', 'e'], index = [2, 3, 4])# 以df1的index为准,df2没有的index填充NaN,df2有但df1没有的index直接抛弃res = pd.concat([df1, df2], axis = 1, join_axes = [df1.index])print(res)

4. append(添加数据)


import pandas as pdimport numpy as np# appenddf1 = pd.DataFrame(np.ones((3, 4)) * 0, columns = ['a', 'b', 'c', 'd'])df2 = pd.DataFrame(np.ones((3, 4)) * 1, columns = ['a', 'b', 'c', 'd'])df3 = pd.DataFrame(np.ones((3, 4)) * 2, columns = ['a', 'b', 'c', 'd'])# 追加记录,添加记录res = df1.append([df2, df3], ignore_index = True)print(res)# 在df1后添加一条记录s1 = pd.Series([1, 2, 3, 4], index = ['a', 'b', 'c', 'd'])print(s1)res = df1.append(s1, ignore_index = True)print(res)

转载于:https://my.oschina.net/shadowolf/blog/1586217

你可能感兴趣的文章
PHP JSON 数据解析代码
查看>>
Android 启动APP黑屏解决方案
查看>>
windows 2003 远程登录时如何修改管理员密码
查看>>
sql server 2008 评估期已过期解决办法
查看>>
2015第8周三马年除夕
查看>>
UI的重用性
查看>>
What is Split Brain in Oracle Clusterware and Real Application Cluster (文档 ID 1425586.1)
查看>>
php SimpleXML
查看>>
CLR C++ Set Word CustomDocumentProperties
查看>>
会员数据挖掘
查看>>
13、java中8中基本类型
查看>>
纯css3制作写轮眼开眼及进化过程
查看>>
CSS布局框架 960GS 表单排版示例
查看>>
OSX终端 命令行的一些基本操作
查看>>
《一句顶一万句》—— 读后总结
查看>>
判断Set里的元素是否重复、==、equals、hashCode方法研究-代码演示
查看>>
rem设置网页字体大小
查看>>
2015第16周日
查看>>
在ASP.Net和IIS中删除不必要的HTTP响应头[转]
查看>>
爬虫 测试webmagic (一)
查看>>