2018.03.27 pandas concat 和 combin_first使用
# 连接和修补concat、combine_first 沿轴的堆叠连接
# 连接concat
import pandas as pd
import numpy as np
s1 = pd.Series([1,2,3])
s2 = pd.Series([2,3,4])
s3 = pd.Series([1,2,3],index=['a','c','h'])
s4 = pd.Series([2,3,4],index=['b','e','d'])
print(s1)
print(s2)
print(pd.concat([s1,s2]))#直接堆接
print(pd.concat([s3,s4]).sort_index())#排序
print('-------')
#默认axis = 0,行+行 print(pd.concat([s1,s2],axis=1))
#当axis=0时,列+列 成为dataframe
结果:
0 1
1 2
2 3
dtype: int64
0 2
1 3
2 4
dtype: int64
0 1
1 2
2 3
0 2
1 3
2 4
dtype: int64
a 1
b 2
c 2
d 4
e 3
h 3
dtype: int64
-------
0 1
0 1 2
1 2 3
2 3 4
#连接方式 join join_axs
s5 = pd.Series([1,2,4],index=['a','b','c'])
s6 = pd.Series([2,3,4],index=['b','c','d'])
print(s5)
print(s6)
print(pd.concat([s5,s6],axis=1))
print(pd.concat([s5,s6],axis=1,join='inner'))#两边同时存在
print(pd.concat([s5,s6],axis=1,join_axes=[['a','b','c']]))#以index = ['a','b','c']为基准去判断
#join_axes指定联合的index
结果:
a 1
b 2
c 4
dtype: int64
b 2
c 3
d 4
dtype: int64
0 1
a 1.0 NaN
b 2.0 2.0
c 4.0 3.0
d NaN 4.0
0 1
b 2 2
c 4 3
0 1
a 1 NaN
b 2 2.0
c 4 3.0
#层次索引
print(pd.concat([s5,s6],axis=1,keys=['one','two']))#覆盖列名
print('---')
print(pd.concat([s5,s6],axis=0,keys=['one','two']))
结果:
one two
a 1.0 NaN
b 2.0 2.0
c 4.0 3.0
d NaN 4.0
---
one a 1
b 2
c 4
two b 2
c 3
d 4
dtype: int64
#修补
df1 = pd.DataFrame([[np.nan,3,5],[-1,6,np.nan],[np.nan,7,np.nan]])
df2 = pd.DataFrame([[-42.6,np.nan,-8.2],[-5,1.6,4]],index=[1,2])
print(df1)
print(df2)
print(df1.combine_first(df2))#按照索引使用df2中的值填补df1z中缺失的内容
#根据index df1的空值被df2代替
#如果df2的index多余df1,则更新到df1上,比如index=['a',1] df1.update(df2)
print(df1)
#直接用df2的值覆盖df1
结果:
0 1 2
0 NaN 3 5.0
1 -1.0 6 NaN
2 NaN 7 NaN
0 1 2
1 -42.6 NaN -8.2
2 -5.0 1.6 4.0
0 1 2
0 NaN 3 5.0
1 -1.0 6 -8.2
2 -5.0 7 4.0
0 1 2
0 NaN 3.0 5.0
1 -42.6 6.0 -8.2
2 -5.0 1.6 4.0
2018.03.27 pandas concat 和 combin_first使用的更多相关文章
- 2018.03.27 pandas duplicated 和 replace 使用
#.duplicated / .replace import numpy as np import pandas as pd s = pd.Series([1,1,1,1,1,2,3,3,3,4,4, ...
- 【VSCode】Windows下VSCode编译调试c/c++【更新 2018.03.27】
--------– 2018.03.27 更新--------- 便携版已更新,点此获取便携版 已知BUG:中文目录无法正常调试 用于cpptools 0.15.0插件的配置文件更新 新的launch ...
- 2018.03.27 python pandas merge join 使用
#2.16 合并 merge-join import numpy as np import pandas as pd df1 = pd.DataFrame({'key1':['k0','k1','k2 ...
- 2018/03/27 每日一个Linux命令 之 cron
Cron 用于配置定时任务. -- 环境为 Ubuntu16-04 -- 先说说怎么配置一个简单的定时任务.直观的可以看到效果. 之前在网上查找资料,对Shell编程不熟悉的实在是很头疼,走了不少弯路 ...
- 网易2018.03.27算法岗,三道编程题100%样例AC题解
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/8660814.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- 数据分析03 /基于pandas的数据清洗、级联、合并
数据分析03 /基于pandas的数据清洗.级联.合并 目录 数据分析03 /基于pandas的数据清洗.级联.合并 1. 处理丢失的数据 2. pandas处理空值操作 3. 数据清洗案例 4. 处 ...
- 20172319 2018.03.27-04.05 《Java程序设计》第4周学习总结
20172319 2018.03.27-04.05 <Java程序设计>第4周学习总结 教材学习内容总结 第四章 编写类 类与对象的回顾:对象是有状态的,状态由对象的属性值确定.属性由类中 ...
- 2018/03/31 每日一个Linux命令 之 date
date 命令主要用于查看和修改时间和时区 -- 这里主要学习基本的查看和设置时间和时区的方法. 直接显示日期 date '+%D' 效果 vagrant@hong:~$ date '+%D' 03/ ...
- EZ 2018 03 16 NOIP2018 模拟赛(四)
链接:http://211.140.156.254:2333/contest/64 我去掉了一百多分! 这次的题目怎么说呢,特别水,但是就是出现了一些很逗的错误导致炸裂. 最好笑的是SB的不只我一个: ...
随机推荐
- framebuffer测试程序
/* framebuffer简单测试程序 网上转载 很多次 的程序 :-) */ #include <stdio.h> #include <stdlib.h> #include ...
- 第05课:GDB常用命令详解(中)
本科核心内容: info和thread命令 next.step.util.finish和return命令 5.1info和thread命令 在前面使用info break命令查看当前断点时介绍过,in ...
- 生成不带版本的jar包 不影响deploy
1 How to build maven project without version? 工程pom中增加 <project> ... <build> ... <fin ...
- Spring mvc项目的web.xml以及注释
web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp ...
- 如何使用hook(非注入dll)
#include <Windows.h> #include <iostream> using namespace std; HHOOK mouseHook; LRESULT _ ...
- python selenium 相关操作
selenium : 是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包括IE(7, 8, 9, 10, 11),Mozilla Fi ...
- jvm——参数解释
https://www.oracle.com/technetwork/java/tuning-139912.html#section4.2.5 https://docs.oracle.com/java ...
- layui 源码解读(部分)
<!DOCTYPE html> <head> </head> <body> <input type="button" id=& ...
- druid配置以及监控
1.druid监控的功能: . 数据源 . SQL监控 对执行的MySQL语句进行记录,并记录执行时间.事务次数等 . SQL防火墙 对SQL进行预编译,并统计该条SQL的数据指标 . Web应用 对 ...
- pycharm如何添加固定代码块
1. file -- settings -- 搜索框输入live,找到 Live Templates 2. 选择你要添加到哪个语言中去,打开python组,并点击右上角 “+”,选择 1.Live T ...