17-numpy笔记-莫烦pandas-5
代码
import pandas as pd
import numpy as np left=pd.DataFrame({'key':['K0','K1','K2','K3'],
'A':['A0','A1','A3','A3'],
'B':['B0','B1','B2','B3'],}) right=pd.DataFrame({'key':['K0','K1','K2','K3'],
'C':['C0','C1','C3','C3'],
'D':['D0','D1','D2','D3'],}) print('-1-')
print(left)
print(right) res = pd.merge(left,right,on='key')
print(res) left=pd.DataFrame({'key1':['K0','K0','K1','K2'],
'key2':['K0','K1','K0','K1'],
'A':['A0','A1','A3','A3'],
'B':['B0','B1','B2','B3'],}) right=pd.DataFrame({'key1':['K0','K1','K1','K2'],
'key2':['K0','K0','K0','K0'],
'C':['C0','C1','C3','C3'],
'D':['D0','D1','D2','D3'],}) print('-2-')
res = pd.merge(left,right,on=['key1','key2'])
print(left)
print(right)
print(res) # default
print('-3-')
res = pd.merge(left,right,on=['key1','key2'],how='inner')
print(left)
print(right)
print(res) print('-4-')
res = pd.merge(left,right,on=['key1','key2'],how='outer')
print(left)
print(right)
print(res) print('-5-')
res = pd.merge(left,right,on=['key1','key2'],how='right')
print(left)
print(right)
print(res) print('-6-')
res = pd.merge(left,right,on=['key1','key2'],how='left')
print(left)
print(right)
print(res) print('-7-')
df1 = pd.DataFrame({'col1':[0,1],'col_left':['a','b']})
df2 = pd.DataFrame({'col1':[1,2,2],'col_right':[2,2,2]})
print(df1)
print(df2)
res = pd.merge(df1,df2,on='col1',how='outer',indicator=True)
print(res) res = pd.merge(df1,df2,on='col1',how='outer',indicator=True)
print(res) res = pd.merge(df1,df2,on='col1',how='outer',indicator='indicator_column')
print(res) df1 = pd.DataFrame({'A':['A0','A1','A2'],
'B':['B0','B1','B2']},
index=['K0','K1','K2']) df2 = pd.DataFrame({'C':['C0','C1','C2'],
'D':['D0','D1','D2']},
index=['K0','K1','K2']) print(df1)
print(df2) print('-8-')
res=pd.merge(left,right,left_index=True,right_index=True,how='outer')
print(res) print('-9-')
res=pd.merge(left,right,left_index=True,right_index=True,how='inner')
print(res) boys = pd.DataFrame({'k':['K0','K1','K2'],'age':[1,2,3]})
girls = pd.DataFrame({'k':['K0','K0','K3'],'age':[4,5,6]}) print('-10-')
print(boys)
print(girls) res = pd.merge(boys, girls, on='k', suffixes=['_boy','_girl'],how='inner')
print(res) res = pd.merge(boys, girls, on='k', suffixes=['_boy','_girl'],how='outer')
print(res)
输出
-1-
key A B
0 K0 A0 B0
1 K1 A1 B1
2 K2 A3 B2
3 K3 A3 B3
key C D
0 K0 C0 D0
1 K1 C1 D1
2 K2 C3 D2
3 K3 C3 D3
key A B C D
0 K0 A0 B0 C0 D0
1 K1 A1 B1 C1 D1
2 K2 A3 B2 C3 D2
3 K3 A3 B3 C3 D3
-2-
key1 key2 A B
0 K0 K0 A0 B0
1 K0 K1 A1 B1
2 K1 K0 A3 B2
3 K2 K1 A3 B3
key1 key2 C D
0 K0 K0 C0 D0
1 K1 K0 C1 D1
2 K1 K0 C3 D2
3 K2 K0 C3 D3
key1 key2 A B C D
0 K0 K0 A0 B0 C0 D0
1 K1 K0 A3 B2 C1 D1
2 K1 K0 A3 B2 C3 D2
-3-
key1 key2 A B
0 K0 K0 A0 B0
1 K0 K1 A1 B1
2 K1 K0 A3 B2
3 K2 K1 A3 B3
key1 key2 C D
0 K0 K0 C0 D0
1 K1 K0 C1 D1
2 K1 K0 C3 D2
3 K2 K0 C3 D3
key1 key2 A B C D
0 K0 K0 A0 B0 C0 D0
1 K1 K0 A3 B2 C1 D1
2 K1 K0 A3 B2 C3 D2
-4-
key1 key2 A B
0 K0 K0 A0 B0
1 K0 K1 A1 B1
2 K1 K0 A3 B2
3 K2 K1 A3 B3
key1 key2 C D
0 K0 K0 C0 D0
1 K1 K0 C1 D1
2 K1 K0 C3 D2
3 K2 K0 C3 D3
key1 key2 A B C D
0 K0 K0 A0 B0 C0 D0
1 K0 K1 A1 B1 NaN NaN
2 K1 K0 A3 B2 C1 D1
3 K1 K0 A3 B2 C3 D2
4 K2 K1 A3 B3 NaN NaN
5 K2 K0 NaN NaN C3 D3
-5-
key1 key2 A B
0 K0 K0 A0 B0
1 K0 K1 A1 B1
2 K1 K0 A3 B2
3 K2 K1 A3 B3
key1 key2 C D
0 K0 K0 C0 D0
1 K1 K0 C1 D1
2 K1 K0 C3 D2
3 K2 K0 C3 D3
key1 key2 A B C D
0 K0 K0 A0 B0 C0 D0
1 K1 K0 A3 B2 C1 D1
2 K1 K0 A3 B2 C3 D2
3 K2 K0 NaN NaN C3 D3
-6-
key1 key2 A B
0 K0 K0 A0 B0
1 K0 K1 A1 B1
2 K1 K0 A3 B2
3 K2 K1 A3 B3
key1 key2 C D
0 K0 K0 C0 D0
1 K1 K0 C1 D1
2 K1 K0 C3 D2
3 K2 K0 C3 D3
key1 key2 A B C D
0 K0 K0 A0 B0 C0 D0
1 K0 K1 A1 B1 NaN NaN
2 K1 K0 A3 B2 C1 D1
3 K1 K0 A3 B2 C3 D2
4 K2 K1 A3 B3 NaN NaN
-7-
col1 col_left
0 0 a
1 1 b
col1 col_right
0 1 2
1 2 2
2 2 2
col1 col_left col_right _merge
0 0 a NaN left_only
1 1 b 2.0 both
2 2 NaN 2.0 right_only
3 2 NaN 2.0 right_only
col1 col_left col_right _merge
0 0 a NaN left_only
1 1 b 2.0 both
2 2 NaN 2.0 right_only
3 2 NaN 2.0 right_only
col1 col_left col_right indicator_column
0 0 a NaN left_only
1 1 b 2.0 both
2 2 NaN 2.0 right_only
3 2 NaN 2.0 right_only
A B
K0 A0 B0
K1 A1 B1
K2 A2 B2
C D
K0 C0 D0
K1 C1 D1
K2 C2 D2
-8-
key1_x key2_x A B key1_y key2_y C D
0 K0 K0 A0 B0 K0 K0 C0 D0
1 K0 K1 A1 B1 K1 K0 C1 D1
2 K1 K0 A3 B2 K1 K0 C3 D2
3 K2 K1 A3 B3 K2 K0 C3 D3
-9-
key1_x key2_x A B key1_y key2_y C D
0 K0 K0 A0 B0 K0 K0 C0 D0
1 K0 K1 A1 B1 K1 K0 C1 D1
2 K1 K0 A3 B2 K1 K0 C3 D2
3 K2 K1 A3 B3 K2 K0 C3 D3
-10-
k age
0 K0 1
1 K1 2
2 K2 3
k age
0 K0 4
1 K0 5
2 K3 6
k age_boy age_girl
0 K0 1 4
1 K0 1 5
k age_boy age_girl
0 K0 1.0 4.0
1 K0 1.0 5.0
2 K1 2.0 NaN
3 K2 3.0 NaN
4 K3 NaN 6.0
17-numpy笔记-莫烦pandas-5的更多相关文章
- 16-numpy笔记-莫烦pandas-4
代码 import pandas as pd import numpy as np dates = pd.date_range('20130101', periods=6) df=pd.DataFra ...
- 15-numpy笔记-莫烦pandas-3
代码 import pandas as pd import numpy as np dates = pd.date_range('20130101', periods=6) df=pd.DataFra ...
- 14-numpy笔记-莫烦pandas-2
代码 import pandas as pd import numpy as np dates = pd.date_range('20130101', periods=6) df=pd.DataFra ...
- 18-numpy笔记-莫烦pandas-6-plot显示
代码 import pandas as pd import numpy as np import matplotlib.pyplot as plt data = pd.Series(np.random ...
- 13-numpy笔记-莫烦pandas-1
代码 import pandas as pd import numpy as np s = pd.Series([1,3,6,np.nan, 44,1]) print('-1-') print(s) ...
- 11-numpy笔记-莫烦基础操作1
代码 import numpy as np array = np.array([[1,2,5],[3,4,6]]) print('-1-') print('数组维度', array.ndim) pri ...
- 12-numpy笔记-莫烦基本操作2
代码 import numpy as np A = np.arange(3,15) print('-1-') print(A) print('-2-') print(A[3]) A = np.aran ...
- Python pandas & numpy 笔记
记性不好,多记录些常用的东西,真·持续更新中::先列出一些常用的网址: 参考了的 莫烦python pandas DOC numpy DOC matplotlib 常用 习惯上我们如此导入: impo ...
- tensorflow学习笔记-bili莫烦
bilibili莫烦tensorflow视频教程学习笔记 1.初次使用Tensorflow实现一元线性回归 # 屏蔽警告 import os os.environ[' import numpy as ...
随机推荐
- 安装QTP之后造成环境变量java冲突问题的解决方案
参考:http://www.cnblogs.com/yhcreak/p/6340125.html
- luoguP4151 [WC2011]最大XOR和路径
题意 这题有点神啊. 首先考虑注意这句话: 路径可以重复经过某些点或边,当一条边在路径中出现了多次时,其权值在计算 XOR 和时也要被计算相应多的次数 也就是说如果出现下面的情况: 我们可以通过异或上 ...
- NOIP模拟赛 最佳组合
题目描述 Description \(Bzeroth\) 大陆最终还是覆灭了,所以你需要为地灾军团服务了. 地灾军团军师黑袍不擅长写题面,所以你只需要看简化版的题意即可. 给定 \(3\) 个长度均为 ...
- (十八)golang--defer关键字
在函数中,程序员经常需要创建资源(比如,数据库连接,文件句柄,锁等),为了在函数执行完毕后,及时释放资源,go设计者提供defer(延时机制) 用defer申明的语句不会立即执行,而是被存入到defe ...
- 自动化API之一 自动生成Mysql数据库的微服务API
本文演示如何利用Uniconnector平台,自动生成Mysql数据库的API,节约开发人员编写后台API的时间.使用生成API的前提是开发者有 自己的数据库,有数据库的管理权限,并能通过外网 ...
- C++:class
class 类是C++的一个重要概念,也是面向对象的一个重要内容.类的行为类似结构体,但功能比结构体的更强大.类是定义该类对象的一个模板,它告诉我们,一个类应该具有什么内容. 声明.定义 类用关键字c ...
- win10怎么打开服务界面?
在键盘上同时点击“win+R”键弹出运行界面,输入services.msc,按确定. 这时就弹出了服务的界面,如图:
- IDEA帮助文档快捷键ctrl+q 查看类 方法 变量 帮助文档 注释 快捷键
IDEA查看类 成员变量 局部变量注释快捷键,Ctrl +Q 查看帮助文档 实际项目中,通常一个类中的代码都不少,而且有很多的变量 那么如何快速知道这个变量的一些信息,比如类型,定义? 比如在第50 ...
- 短的 Guid 帮助类
直接贴代码了: /// <summary> /// 短的 Guid 帮助类 /// </summary> public class ShortGuidHelper { #reg ...
- 使用 jQuery.TypeAhead 让文本框自动完成 (三)(服务器返回 JSON 复杂对象数组)
项目地址:https://github.com/twitter/typeahead.js 直接贴代码了: @section headSection { <script type="te ...