python Dataframe 合并與去重詳情
1.合并
1.1 結(jié)構(gòu)合并
將兩個結(jié)構(gòu)相同的數(shù)據(jù)合并
1.1.1 concat函數(shù)
函數(shù)配置:
concat([dataFrame1, dataFrame2,…], index_ingore=False)
參數(shù)說明:index_ingore=False(表示合并的索引不延續(xù)),index_ingore=True(表示合并的索引可延續(xù))
實例:
import pandas as pd import numpy as np # 創(chuàng)建一個十行兩列的二維數(shù)據(jù) df = pd.DataFrame(np.random.randint(0, 10, (3, 2)), columns=['A', 'B']) # 將數(shù)據(jù)拆分成兩份,并保存在列表中 data_list = [df[0:2], df[3:]] # 索引值不延續(xù) df1 = pd.concat(data_list, ignore_index=False) # 索引值延續(xù) df2 = pd.concat(data_list, ignore_index=True)
返回結(jié)果:
----------------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出現(xiàn),索引不連續(xù)
4 1 8
----------------df2--------------------------
A B
0 7 8
1 7 3
2 4 0
3 1 8
1.1.2 append函數(shù)
函數(shù)配置:
df.append(df1, index_ignore=True)
參數(shù)說明:index_ingore=False(表示索引不延續(xù)),index_ingore=True(表示索引延續(xù))
實例:
import pandas as pd import numpy as np # 創(chuàng)建一個五行兩列的二維數(shù)組 df = pd.DataFrame(np.random.randint(0, 10, (5, 2)), columns=['A', 'B']) # 創(chuàng)建要追加的數(shù)據(jù) narry = np.random.randint(0, 10, (3, 2)) data_list = pd.DataFrame(narry, columns=['A', 'B']) # 合并數(shù)據(jù) df1 = df.append(data_list, ignore_index=True)
返回結(jié)果:
----------------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 字段合并
將同一個數(shù)據(jù)不同列合并
參數(shù)配置:
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, )參數(shù)說明:
| 參數(shù) | 說明 |
|---|---|
| how | 連接方式:inner、left、right、outer,默認為 inner |
| on | 用于連接的列名 |
| left_on | 左表用于連接的列名 |
| right_on | 右表用于連接的列名 |
| Left_index | 是否使用左表的行索引作為連接鍵,默認為False |
| Right_index | 是否使用右表的行索引作為連接鍵,默認為False |
| sort | 默認為False,將合并的數(shù)據(jù)進行排序 |
| copy | 默認為True??偸菍?shù)據(jù)復制到數(shù)據(jù)結(jié)構(gòu)中,設置為False可以提高性能 |
| suffixes | 存在相同列名時在列名后面添加的后綴,默認為(’_x’, ‘_y’) |
| indicator | 顯示合并數(shù)據(jù)中數(shù)據(jù)來自哪個表 |
實例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) # 合并時默認以重復列并作為合并依據(jù)結(jié)果展示:
----------------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')結(jié)果展示:
----------------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.去重
參數(shù)配置:
data.drop_duplicates(subset=['A','B'],keep='first',inplace=True)
參數(shù)說明:
| 參數(shù) | 說明 |
|---|---|
| subset | 列名,可選,默認為None |
| keep | {‘first’, ‘last’, False}, 默認值 ‘first’ |
| first | 保留第一次出現(xiàn)的重復行,刪除后面的重復行 |
| last | 刪除重復項,除了最后一次出現(xiàn) |
| False | 刪除所有重復項 |
| inplace | 布爾值,默認為False,是否直接在原數(shù)據(jù)上刪除重復項或刪除重復項后返回副本。(inplace=True表示直接在原來的DataFrame上刪除重復項,而默認值False表示生成一個副本。) |
實例:
去除完全重復的行數(shù)據(jù)
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()結(jié)果展示:
---------------去重前的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 去除某幾列重復的行數(shù)據(jù)
data.drop_duplicates(subset=[‘A’,‘B’],keep=‘first’,inplace=True)
df.drop_duplicates(subset=['brand'])
結(jié)果展示:
brand style rating
0 Yum Yum cup 4.0
2 Indomie cup 3.5
使用 keep刪除重復項并保留最后一次出現(xiàn)
df.drop_duplicates(subset=['brand', 'style'], keep='last')
結(jié)果展示:
brand style rating
1 Yum Yum cup 4.0
2 Indomie cup 3.5
4 Indomie pack 5.0
到此這篇關(guān)于python Dataframe 合并與去重詳情的文章就介紹到這了,更多相關(guān)python Dataframe內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實現(xiàn)將Unicode轉(zhuǎn)換為ASCII
這篇文章主要為大家詳細介紹了系統(tǒng)編碼的不同方法以及如何利用Python實現(xiàn)將Unicode轉(zhuǎn)換為?ASCII,文中的示例代碼講解詳細,有需要的小伙伴可以學習一下2023-10-10
python heic后綴圖片文件轉(zhuǎn)換成jpg格式的操作
這篇文章主要介紹了python heic后綴圖片文件轉(zhuǎn)換成jpg格式的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03
python調(diào)用百度地圖WEB服務API獲取地點對應坐標值
這篇文章主要為大家詳細介紹了python調(diào)用百度地圖WEB服務API獲取地點對應坐標值,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-01-01
Python開發(fā)時報TypeError:?‘int‘?object?is?not?iterable錯誤的解決方
Python寫循環(huán)程序的時候遇到TypeError:'int'object is not iterable,所以下面這篇文章主要給大家介紹了關(guān)于Python開發(fā)時報TypeError:'int'?object?is?not?iterable錯誤的解決方式,需要的朋友可以參考下2022-06-06

