博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
pandas的基本用法(六)——合并数据
阅读量:3981 次
发布时间:2019-05-24

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

文章作者:Tyan

博客:  |   | 

本文主要是关于pandas的一些基本用法。

#!/usr/bin/env python# _*_ coding: utf-8 _*_import pandas as pdimport numpy as np# Test 1# 创建DataFramedf1 = 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'])print df1print df2print df3# Test 1 result     a    b    c    d0  0.0  0.0  0.0  0.01  0.0  0.0  0.0  0.02  0.0  0.0  0.0  0.0     a    b    c    d0  1.0  1.0  1.0  1.01  1.0  1.0  1.0  1.02  1.0  1.0  1.0  1.0     a    b    c    d0  2.0  2.0  2.0  2.01  2.0  2.0  2.0  2.02  2.0  2.0  2.0  2.0# Test 2# 竖向合并三个DataFrameres = pd.concat([df1, df2, df3], axis = 0)print res# 横向合并三个DataFrameres = pd.concat([df1, df2, df3], axis = 1)print res# 合并的同时index重新排序res = pd.concat([df1, df2, df3], axis = 0, ignore_index = True)print res# Test 2 result     a    b    c    d0  0.0  0.0  0.0  0.01  0.0  0.0  0.0  0.02  0.0  0.0  0.0  0.00  1.0  1.0  1.0  1.01  1.0  1.0  1.0  1.02  1.0  1.0  1.0  1.00  2.0  2.0  2.0  2.01  2.0  2.0  2.0  2.02  2.0  2.0  2.0  2.0     a    b    c    d    a    b    c    d    a    b    c    d0  0.0  0.0  0.0  0.0  1.0  1.0  1.0  1.0  2.0  2.0  2.0  2.01  0.0  0.0  0.0  0.0  1.0  1.0  1.0  1.0  2.0  2.0  2.0  2.02  0.0  0.0  0.0  0.0  1.0  1.0  1.0  1.0  2.0  2.0  2.0  2.0     a    b    c    d0  0.0  0.0  0.0  0.01  0.0  0.0  0.0  0.02  0.0  0.0  0.0  0.03  1.0  1.0  1.0  1.04  1.0  1.0  1.0  1.05  1.0  1.0  1.0  1.06  2.0  2.0  2.0  2.07  2.0  2.0  2.0  2.08  2.0  2.0  2.0  2.0# Test 3# 创建DataFrame, 部分索引重合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 = [2, 3, 4])print df1print df2# 直接合并, 默认的join模式为outer, 与pd.concat([df1, df2])是一样的res = pd.concat([df1, df2], join = 'outer')print res# 合并模式为innerres = pd.concat([df1, df2], join = 'inner')print res# 合并模式为inner, 同时重新排序res = pd.concat([df1, df2], join = 'inner', ignore_index = True)print res# Test 3 result     a    b    c    d1  0.0  0.0  0.0  0.02  0.0  0.0  0.0  0.03  0.0  0.0  0.0  0.0     b    c    d    e2  1.0  1.0  1.0  1.03  1.0  1.0  1.0  1.04  1.0  1.0  1.0  1.0     a    b    c    d    e1  0.0  0.0  0.0  0.0  NaN2  0.0  0.0  0.0  0.0  NaN3  0.0  0.0  0.0  0.0  NaN2  NaN  1.0  1.0  1.0  1.03  NaN  1.0  1.0  1.0  1.04  NaN  1.0  1.0  1.0  1.0     b    c    d1  0.0  0.0  0.02  0.0  0.0  0.03  0.0  0.0  0.02  1.0  1.0  1.03  1.0  1.0  1.04  1.0  1.0  1.0     b    c    d0  0.0  0.0  0.01  0.0  0.0  0.02  0.0  0.0  0.03  1.0  1.0  1.04  1.0  1.0  1.05  1.0  1.0  1.0# Test 4# 横向合并res = pd.concat([df1, df2], axis = 1)print res# 横向合并, 按照df1的index, 忽略df2不一致的indexres = pd.concat([df1, df2], axis = 1, join_axes = [df1.index])print res# Test 4 result     a    b    c    d    b    c    d    e1  0.0  0.0  0.0  0.0  NaN  NaN  NaN  NaN2  0.0  0.0  0.0  0.0  1.0  1.0  1.0  1.03  0.0  0.0  0.0  0.0  1.0  1.0  1.0  1.04  NaN  NaN  NaN  NaN  1.0  1.0  1.0  1.0     a    b    c    d    b    c    d    e1  0.0  0.0  0.0  0.0  NaN  NaN  NaN  NaN2  0.0  0.0  0.0  0.0  1.0  1.0  1.0  1.03  0.0  0.0  0.0  0.0  1.0  1.0  1.0  1.0# Test 5# 创建DataFramedf1 = 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'])# appendprint df1.append(df2, ignore_index = True)# append 多个DataFrameprint df1.append([df2, df3], ignore_index = True)# Test 5 result     a    b    c    d0  0.0  0.0  0.0  0.01  0.0  0.0  0.0  0.02  0.0  0.0  0.0  0.03  1.0  1.0  1.0  1.04  1.0  1.0  1.0  1.05  1.0  1.0  1.0  1.0     a    b    c    d0  0.0  0.0  0.0  0.01  0.0  0.0  0.0  0.02  0.0  0.0  0.0  0.03  1.0  1.0  1.0  1.04  1.0  1.0  1.0  1.05  1.0  1.0  1.0  1.06  2.0  2.0  2.0  2.07  2.0  2.0  2.0  2.08  2.0  2.0  2.0  2.0

参考资料

转载地址:http://fjwui.baihongyu.com/

你可能感兴趣的文章
JS清空多文本框|文本域
查看>>
linux常用命令(操作命令)
查看>>
Linux一些经典书籍
查看>>
apache启动报错(98)Address already in use: make_sock: could not bind to address [::]:80
查看>>
linux kill用法、killall、pkill、xkill
查看>>
Python笔记——排序算法的实现
查看>>
jQuery数据显示插件整合实现代码
查看>>
python时区设置——pytz模块
查看>>
用datetime和pytz来转换时区
查看>>
python解决导出excel文件时中文文件名乱码
查看>>
Django操作NOSQL(MongoDB)数据库
查看>>
Failed to load JavaHL Library
查看>>
linux学习方法
查看>>
linux中nohup命令的用法
查看>>
vim代码智能提示功能及相关配置
查看>>
linux常用命令——ps
查看>>
linux常用命令——lsof
查看>>
nginx安装手册
查看>>
Nginx配置文件详细说明
查看>>
Nginx负载均衡
查看>>