import pandas as pd
import numpy as np # merge合并 ,类似于Excel中的vlookup df1 = pd.DataFrame({'key': ['K0', 'K1', 'K2', 'K3'],
'A': ['A0', 'A1', 'A2', 'A3'],
'B': ['B0', 'B1', 'B2', 'B3']})
df2 = pd.DataFrame({'key': ['K0', 'K1', 'K2', 'K3'],
'C': ['C0', 'C1', 'C2', 'C3'],
'D': ['D0', 'D1', 'D2', 'D3']})
df3 = pd.DataFrame({'key1': ['K0', 'K0', 'K2', 'K3'],
'key2': ['K0', 'K1', 'K0', 'K1'],
'A': ['A0', 'A1', 'A2', 'A3'],
'B': ['B0', 'B1', 'B2', 'B3']})
df4 = pd.DataFrame({'key1': ['K0', 'K0', 'K2', 'K3'],
'key2': ['K0', 'K0', 'K0', 'K0'],
'C': ['C0', 'C1', 'C2', 'C3'],
'D': ['D0', 'D1', 'D2', 'D3']})
print(pd.merge(df1,df2,on='key'))
# 第一个DataFrame为拼接后左边的
# 第二个DataFrame为拼接后右边的
# on 为参考键
'''
key A B C D
0 K0 A0 B0 C0 D0
1 K1 A1 B1 C1 D1
2 K2 A2 B2 C2 D2
3 K3 A3 B3 C3 D3
'''
# 多个键连接
print(pd.merge(df3, df4, on=['key1', 'key2']))
# 当两个DataFrame中的key1和key2都相同时,才会连,否则不连
'''
key1 key2 A B C D
0 K0 K0 A0 B0 C0 D0
1 K0 K0 A0 B0 C1 D1
2 K2 K0 A2 B2 C2 D2
'''
# 参数how , 合并方式
# 默认,取交集
print(pd.merge(df3, df4, on=['key1', 'key2'], how='inner'))
print('-' * 8)
'''
key1 key2 A B C D
0 K0 K0 A0 B0 C0 D0
1 K0 K0 A0 B0 C1 D1
2 K2 K0 A2 B2 C2 D2
--------
'''
# 取并集,outer,数据缺失范围NaN
print(pd.merge(df3, df4, on=['key1', 'key2'], how='outer'))
print('-' * 8)
'''
key1 key2 A B C D
0 K0 K0 A0 B0 C0 D0
1 K0 K0 A0 B0 C1 D1
2 K0 K1 A1 B1 NaN NaN
3 K2 K0 A2 B2 C2 D2
4 K3 K1 A3 B3 NaN NaN
5 K3 K0 NaN NaN C3 D3
--------
'''
# 参照df3为参考合并,数据缺失范围NaN
print(pd.merge(df3, df4, on=['key1', 'key2'], how='left'))
print('-' * 8)
'''
key1 key2 A B C D
0 K0 K0 A0 B0 C0 D0
1 K0 K0 A0 B0 C1 D1
2 K0 K1 A1 B1 NaN NaN
3 K2 K0 A2 B2 C2 D2
4 K3 K1 A3 B3 NaN NaN
--------
'''
# 参照df4为参考合并,数据缺失范围NaN
print(pd.merge(df3, df4, on=['key1', 'key2'], how='right'))
print('-' * 8)
'''
key1 key2 A B C D
0 K0 K0 A0 B0 C0 D0
1 K0 K0 A0 B0 C1 D1
2 K2 K0 A2 B2 C2 D2
3 K3 K0 NaN NaN C3 D3
--------
'''
# 参数left_on,right_on,left_index, right_index ,当键不为一个列时,可以单独设置左键与右键
df5 = pd.DataFrame({'lkey': list('bbacaab'),
'data1': range(7)})
df6 = pd.DataFrame({'rkey': list('abd'),
'date2': range(3)})
print(df5)
print(df6)
print(pd.merge(df5,df6,left_on='lkey',right_on='rkey'))
'''
lkey data1
0 b 0
1 b 1
2 a 2
3 c 3
4 a 4
5 a 5
6 b 6
rkey date2
0 a 0
1 b 1
2 d 2
lkey data1 rkey date2
0 b 0 b 1
1 b 1 b 1
2 b 6 b 1
3 a 2 a 0
4 a 4 a 0
5 a 5 a 0
''' # concat() 连接,默认axis=0 行+行,当axis=1时,列+列 成为Dataframe
s1 = pd.Series([2, 3, 4])
s2 = pd.Series([1, 2, 3])
print(pd.concat([s1, s2]))
'''
0 2
1 3
2 4
0 1
1 2
2 3
dtype: int64
'''
print(pd.concat([s1,s2],axis=1))
'''
0 1
0 2 1
1 3 2
2 4 3
'''
snew = pd.concat([s1, s2], axis=1)
snew.reset_index(inplace=True)
print(snew)
'''
index 0 1
0 0 2 1
1 1 3 2
2 2 4 3
'''
snew2 = pd.concat([s1, s2], axis=1)
snew2.reset_index(inplace=True, drop=True)
print(snew2)
'''
0 1
0 2 1
1 3 2
2 4 3
''' # 去重 .duplicated()
s3 = pd.Series([1, 2, 2, 4, 4, 6, 7, 6, 87])
# 判断是否重复
print(s3.duplicated())
'''
0 False
1 False
2 True
3 False
4 True
5 False
6 False
7 True
8 False
dtype: bool
'''
# 取出重复的值
s4 = s3[s3.duplicated()]
print(s4)
# 取出唯一的元素
s5 = s3[s3.duplicated() == False]
print(s5)
'''
0 1
1 2
3 4
5 6
6 7
8 87
dtype: int64
'''
s5 = s3.drop_duplicates()
# 可以通过设置参数:inplace控制是否替换原先的值
print(s5)
'''
0 1
1 2
3 4
5 6
6 7
8 87
dtype: int64
'''
df7 = pd.DataFrame({'key1':['a','a',3,4,3],
'key2':['a','a','b','b',5]})
print(df7.duplicated())
# 按行检测,第二次出现时,返回True
'''
0 1
1 2
3 4
5 6
6 7
8 87
dtype: int64
'''
# 今查看key2列
print(df7['key2'].duplicated())
'''
0 False
1 True
2 False
3 True
4 False
Name: key2, dtype: bool
'''
# 直接去重
print(df7.drop_duplicates())
'''
key1 key2
0 a a
2 3 b
3 4 b
4 3 5
'''
print(df7['key2'].drop_duplicates())
'''
0 a
2 b
4 5
Name: key2, dtype: object
''' # 替换 .replace()
s6 = pd.Series(list('askjdghs'))
# 一次性替换一个值
# print(s6.replace('s','dsd'))
'''
0 a
1 dsd
2 k
3 j
4 d
5 g
6 h
7 dsd
dtype: object
'''
# 一次性替换多个值
print(s6.replace(['a','s'],np.nan))
'''
0 NaN
1 NaN
2 k
3 j
4 d
5 g
6 h
7 NaN
dtype: object
'''
# 通过字典的形式替换值
print(s6.replace({'a':np.nan}))
'''
0 NaN
1 s
2 k
3 j
4 d
5 g
6 h
7 s
dtype: object '''

pandas的合并、连接、去重、替换的更多相关文章

  1. 04. Pandas 3| 数值计算与统计、合并连接去重分组透视表文件读取

    1.数值计算和统计基础 常用数学.统计方法 数值计算和统计基础 基本参数:axis.skipna df.mean(axis=1,skipna=False)  -->> axis=1是按行来 ...

  2. Pandas | 19 合并/连接

    Pandas具有功能全面的高性能内存中连接操作,与SQL等关系数据库非常相似.Pandas提供了一个单独的merge()函数,作为DataFrame对象之间所有标准数据库连接操作的入口 - pd.me ...

  3. SQL连接操作符介绍(循环嵌套, 哈希匹配和合并连接)

    今天我将介绍在SQLServer 中的三种连接操作符类型,分别是:循环嵌套.哈希匹配和合并连接.主要对这三种连接的不同.复杂度用范例的形式一一介绍. 本文中使用了示例数据库AdventureWorks ...

  4. 排序合并连接(sort merge join)的原理

    排序合并连接(sort merge join)的原理 排序合并连接(sort merge join)的原理     排序合并连接(sort merge join)       访问次数:两张表都只会访 ...

  5. oracle表连接------>排序合并连接(Merge Sort Join)

    排序合并连接 (Sort Merge Join)是一种两个表在做连接时用排序操作(Sort)和合并操作(Merge)来得到连接结果集的连接方法. 对于排序合并连接的优缺点及适用场景例如以下: a,通常 ...

  6. pandas列合并为一行

    将dataframe利用pandas列合并为一行,类似于sql的GROUP_CONCAT函数.例如如下dataframe id_part pred pred_class v_id 0 d 0 0.12 ...

  7. JS 两个对象数组合并并去重

    JS两个对象数组合并并去重 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...

  8. PHP数组合并和去重的函数有哪些

    PHP数组合并和去重的函数有哪些 一.总结 一句话总结:合并:array_merge() array_merge_recursive() +号:去重:array_flip() array_unique ...

  9. oracle 表连接 - sort merge joins 排序合并连接

    https://blog.csdn.net/dataminer_2007/article/details/41907581一. sort merge joins连接(排序合并连接) 原理 指的是两个表 ...

  10. arcgis中的Join(合并连接)和Relate(关联连接)

    arcgis中的Join(合并连接)和Relate(关联连接) 一.区别 1.连接关系不一样. Relate(关联连接)方式连接的两个表之间的记录可以是“一对一”.“多对一”.“一对多”的关系 Joi ...

随机推荐

  1. hdu 5201 The Monkey King【容斥原理+组合数学】

    原来我一开始以为的\( O(n^2) \)是调和级数\( O(nlog_2n) \)的! 首先枚举猴王的桃子个数\( x \),然后使用容斥原理,枚举有至少\( k \)个不满足的条件,那么这\( k ...

  2. 洛谷P3287 [SCOI2014]方伯伯的玉米田(树状数组)

    传送门 首先要发现,每一次选择拔高的区间都必须包含最右边的端点 为什么呢?因为如果拔高了一段区间,那么这段区间对于它的左边是更优的,对它的右边会更劣,所以我们每一次选的区间都得包含最右边的端点 我们枚 ...

  3. Reshapeing operations

    Reshapeing operations Suppose we have the following tensor: t = torch.tensor([ [1,1,1,1], [2,2,2,2], ...

  4. 购买阿里云ECS+安装宝塔面板+Mac下怎么连接阿里云ECS服务器

    1.购买阿里云ECS 2.重置实例密码 这个有点对用户不友好,实际意思就是设置服务器的root登录密码 3.配置安全组放行端口 因为服务器需要从宝塔网站download安装包,包括一些常用的服务比如S ...

  5. Web自动化测试框架-PO模式

    Web自动化测试框架(WebTestFramework)是基于Selenium框架且采用PageObject设计模式进行二次开发形成的框架. 一.适用范围:传统Web功能自动化测试.H5功能自动化测试 ...

  6. Palindromic Subsets 数学 + 线段树

    https://www.hackerrank.com/contests/101hack44/challenges/palindromic-subsets 如果有3个a.2个b.1个c. 每个a看成不同 ...

  7. Ubuntu卸载软件包

    sudo apt-get autoremove --purge mysql-server-5.0 ,purge连同配置文件一起删除,autoremove自动卸载依赖包sudo apt-get remo ...

  8. 分享一款强大的图片查看器插件,手机PC 通吃,功能超级齐全!

    一款强大的图片查看器插件,手机PC 通吃,功能超级齐全! 地址:http://photoswipe.com/

  9. 在Paint事件中绘制控件(边框)

    单纯的自己记录,将来会继续添加,侥幸被大家发现了的话请不要太鄙视... private void panel4_Paint(object sender, PaintEventArgs e) { Con ...

  10. Webform 内置对象(Response对象、Request对象,QueryString)

    Response对象:响应请求 Response.Write("<script>alert('添加成功!')</script>"); Response.Re ...