欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Pandas之使用drop_duplicates:去除重復(fù)項(xiàng)

 更新時間:2023年12月19日 09:58:04   作者:小虎AI實(shí)驗(yàn)室  
這篇文章主要介紹了Pandas之使用drop_duplicates:去除重復(fù)項(xiàng)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

前言

本文,我們講述Pandas如何去除重復(fù)項(xiàng)的操作,我們選擇一個評價數(shù)據(jù)集來演示如何刪除特定列上的重復(fù)項(xiàng),如何刪除重復(fù)項(xiàng)并保留最后一次出現(xiàn),以及drop_duplicates的默認(rèn)用法

方法

DataFrame.drop_duplicates(subset=None, keep='first', inplace=False)

返回值

這個drop_duplicate方法是對DataFrame格式的數(shù)據(jù),去除特定列下面的重復(fù)行。

返回刪除重復(fù)行的 DataFrame。

考慮某些列是可選的。

索引(包括時間索引)將被忽略。

參數(shù)

返回DataFrame格式的數(shù)據(jù)。

  • subset : column label or sequence of labels, optional
  • 用來指定特定的列,默認(rèn)所有列
  • keep : {‘first’, ‘last’, False}, default ‘first’
  • 刪除重復(fù)項(xiàng)并保留第一次出現(xiàn)的項(xiàng)
  • inplace : boolean, default False
  • 是直接在原來數(shù)據(jù)上修改還是保留一個副本

實(shí)驗(yàn)

構(gòu)建包含拉面評級的數(shù)據(jù)集

df = pd.DataFrame({
    'brand': ['Yum Yum', 'Yum Yum', 'Indomie', 'Indomie', 'Indomie'],
    'style': ['cup', 'cup', 'cup', 'pack', 'pack'],
    'rating': [4, 4, 3.5, 15, 5]
})

數(shù)據(jù)集數(shù)據(jù)格式

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

默認(rèn)情況下,它會根據(jù)所有列刪除重復(fù)的行

df.drop_duplicates()

brand style rating
0 Yum Yum cup 4.0
2 Indomie cup 3.5
3 Indomie pack 15.0
4 Indomie pack 5.0

要刪除特定列上的重復(fù)項(xiàng),請使用subset

df.drop_duplicates(subset=['brand'])

brand style rating
0 Yum Yum cup 4.0
2 Indomie cup 3.5

要刪除重復(fù)項(xiàng)并保留最后一次出現(xiàn),請使用 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

1

總結(jié)

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論