網站首頁 編程語言 正文
1.合并
1.1 結構合并
將兩個結構相同的數據合并
1.1.1 concat函數
函數配置:
concat([dataFrame1, dataFrame2,…], index_ingore=False)
參數說明:index_ingore=False(表示合并的索引不延續),index_ingore=True(表示合并的索引可延續)
實例:
import pandas as pd
import numpy as np
# 創建一個十行兩列的二維數據
df = pd.DataFrame(np.random.randint(0, 10, (3, 2)), columns=['A', 'B'])
# 將數據拆分成兩份,并保存在列表中
data_list = [df[0:2], df[3:]]
# 索引值不延續
df1 = pd.concat(data_list, ignore_index=False)
# 索引值延續
df2 = pd.concat(data_list, ignore_index=True)
返回結果:
----------------df--------------------------
? ?A ?B
0 ?7 ?8
1 ?7 ?3
2 ?5 ?9
3 ?4 ?0
4 ?1 ?8
----------------df1--------------------------
? ?A ?B
0 ?7 ?8
1 ?7 ?3
3 ?4 ?0# -------------->這里并沒有2出現,索引不連續
4 ?1 ?8
----------------df2--------------------------
? ?A ?B
0 ?7 ?8
1 ?7 ?3
2 ?4 ?0
3 ?1 ?8
1.1.2 append函數
函數配置:
df.append(df1, index_ignore=True)
參數說明:index_ingore=False(表示索引不延續),index_ingore=True(表示索引延續)
實例:
import pandas as pd
import numpy as np
# 創建一個五行兩列的二維數組
df = pd.DataFrame(np.random.randint(0, 10, (5, 2)), columns=['A', 'B'])
# 創建要追加的數據
narry = np.random.randint(0, 10, (3, 2))
data_list = pd.DataFrame(narry, columns=['A', 'B'])
# 合并數據
df1 = df.append(data_list, ignore_index=True)
返回結果:
----------------df--------------------------
? ?A ?B
0 ?5 ?6
1 ?1 ?2
2 ?5 ?3
3 ?1 ?8
4 ?1 ?2
----------------df1--------------------------
? ?A ?B
0 ?5 ?6
1 ?1 ?2
2 ?5 ?3
3 ?1 ?8
4 ?1 ?2
5 ?8 ?1
6 ?3 ?5
7 ?1 ?1
1.2 字段合并
將同一個數據不同列合并
參數配置:
pd.merge( left, right, how="inner", on=None, left_on=None, right_on=None, left_index=False, right_index=False, sort=False, suffixes=("_x", "_y"), copy=True, indicator=False, validate=None, )
參數說明:
參數 | 說明 |
---|---|
how | 連接方式:inner、left、right、outer,默認為 inner |
on | 用于連接的列名 |
left_on | 左表用于連接的列名 |
right_on | 右表用于連接的列名 |
Left_index | 是否使用左表的行索引作為連接鍵,默認為False |
Right_index | 是否使用右表的行索引作為連接鍵,默認為False |
sort | 默認為False,將合并的數據進行排序 |
copy | 默認為True??偸菍祿椭频綌祿Y構中,設置為False可以提高性能 |
suffixes | 存在相同列名時在列名后面添加的后綴,默認為(’_x’, ‘_y’) |
indicator | 顯示合并數據中數據來自哪個表 |
實例1:
import pandas as pd
df1 = pd.DataFrame({'key':['a','b','c'], 'data1':range(3)})
df2 = pd.DataFrame({'key':['a','b','c'], 'data2':range(3)})
df = pd.merge(df1, df2) # 合并時默認以重復列并作為合并依據
結果展示:
----------------df1--------------------------
? key ?data1
0 ? a ? ? ?0
1 ? b ? ? ?1
2 ? c ? ? ?2
----------------df2--------------------------
? key ?data2
0 ? a ? ? ?0
1 ? b ? ? ?1
2 ? c ? ? ?2
----------------df---------------------------
? key ?data1 ?data2
0 ? a ? ? ?0 ? ? ?0
1 ? b ? ? ?1 ? ? ?1
2 ? c ? ? ?2 ? ? ?2
實例2:
# 多鍵連接時將連接鍵組成列表傳入
right=DataFrame({'key1':['foo','foo','bar','bar'],
'key2':['one','one','one','two'],
'lval':[4,5,6,7]})
left=DataFrame({'key1':['foo','foo','bar'],
'key2':['one','two','one'],
'lval':[1,2,3]})
pd.merge(left,right,on=['key1','key2'],how='outer')
結果展示:
----------------right-------------------------
? key1 key2 ?lval
0 ?foo ?one ? ? 4
1 ?foo ?one ? ? 5
2 ?bar ?one ? ? 6
3 ?bar ?two ? ? 7
----------------left--------------------------
? key1 key2 ?lval
0 ?foo ?one ? ? 1
1 ?foo ?two ? ? 2
2 ?bar ?one ? ? 3
----------------df---------------------------
? key1 key2 ?lval_x ?lval_y
0 ?foo ?one ? ? 1.0 ? ? 4.0
1 ?foo ?one ? ? 1.0 ? ? 5.0
2 ?foo ?two ? ? 2.0 ? ? NaN
3 ?bar ?one ? ? 3.0 ? ? 6.0
4 ?bar ?two ? ? NaN ? ? 7.0
?
2.去重
參數配置:
data.drop_duplicates(subset=['A','B'],keep='first',inplace=True)
參數說明:
參數 | 說明 |
---|---|
subset | 列名,可選,默認為None |
keep | {‘first’, ‘last’, False}, 默認值 ‘first’ |
first | 保留第一次出現的重復行,刪除后面的重復行 |
last | 刪除重復項,除了最后一次出現 |
False | 刪除所有重復項 |
inplace | 布爾值,默認為False,是否直接在原數據上刪除重復項或刪除重復項后返回副本。(inplace=True表示直接在原來的DataFrame上刪除重復項,而默認值False表示生成一個副本。) |
實例:
去除完全重復的行數據
data.drop_duplicates(inplace=True)
df = pd.DataFrame({
'brand': ['Yum Yum', 'Yum Yum', 'Indomie', 'Indomie', 'Indomie'],
'style': ['cup', 'cup', 'cup', 'pack', 'pack'],
'rating': [4, 4, 3.5, 15, 5]
})
df.drop_duplicates()
結果展示:
---------------去重前的df---------------------------
brand style rating
0 Yum Yum cup 4.0
1 Yum Yum cup 4.0
2 Indomie cup 3.5
3 Indomie pack 15.0
4 Indomie pack 5.0
---------------去重后的df---------------------------
brand style rating
0 Yum Yum cup 4.0
2 Indomie cup 3.5
3 Indomie pack 15.0
4 Indomie pack 5.0
使用subset 去除某幾列重復的行數據
data.drop_duplicates(subset=[‘A’,‘B’],keep=‘first’,inplace=True)
df.drop_duplicates(subset=['brand'])
結果展示:
brand style rating
0 Yum Yum cup 4.0
2 Indomie cup 3.5
使用 keep刪除重復項并保留最后一次出現
df.drop_duplicates(subset=['brand', 'style'], keep='last')
結果展示:
brand style rating
1 Yum Yum cup 4.0
2 Indomie cup 3.5
4 Indomie pack 5.0
原文鏈接:https://blog.51cto.com/coderusher/5554275
相關推薦
- 2022-07-19 AI與Python人工智能啟發式搜索概念理解_python
- 2021-11-27 Centos7.9搭建自主郵件服務器詳細步驟_Linux
- 2024-01-28 在已有git倉庫的情況下,如何提交修改后的文件
- 2022-08-04 C++中純虛函數的實例詳解_C 語言
- 2022-11-08 PostgreSQL?pg_filenode.map文件介紹_PostgreSQL
- 2022-05-17 IDEA自帶maven報錯maven-default-http-blocker (http://0.
- 2022-07-22 idea 編譯項目后target包沒有resources文件
- 2022-11-04 Android自定義View實現時鐘功能_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支