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的不只我一个: ...
随机推荐
- Delphi 帮助的使用
- ipcs - 提供基于 ipc (Inter-process communication)结构的信息
总览 ipcrm [ shm | msg | sem ] id 描述 ipcrm 将删除指定的资源--指定 id.
- VS编译器问题总结
error C2236: 意外的“class”“CTsgBaseTask”.是否忘记了“;”? 出现这个问题的原因是在引用的一个头文件中定义的一个类最后没有加分号";".
- xss非法字段过滤
import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util. ...
- linux 走三层内网添加静态路由
/etc/sysconfig/network-scripts/ifcfg-eth1 #机器1ip route add 10.24.4.0/24 via 10.90.203.1 dev ens33/et ...
- Jmeter性能测试--自己看到的博客收集
性能测试的场景:https://www.cnblogs.com/little-little-bai/p/10338156.html
- vue项目刷新页面,使数据不丢失(sessionStorage、localStorage、cookie)
vue项目刷新页面时,存储在vuex中的数据会丢失,把他们存到stroage中可以保证不丢失.
- Lambda表达式匿名类实现接口方法
Lamb表达式匿名类实现接口方法 import java.util.ArrayList; public class HandlerDemo{ public static void main(Strin ...
- 通俗理解vue路由的导航钩子中关于next()
1 背景:你乘坐汽车从A景区想赶往B景区(模拟路由A跳转到路由B) 1.next() 你乘坐汽车要从A景区到B景区,路过关卡时,守门人拦下你,你量出了next(),守门人一看没问题,赶紧放行,于是你顺 ...
- CDOJ 203 并查集+优先队列 好题
题目链接 Islands Time Limit: 30000/10000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) S ...