pandas去重函数
pandas.DataFrame.duplicated
duplicated api
- DataFrame.duplicated(subset=None, keep=first)
返回布爾類型的Series結(jié)構(gòu)表示有重復(fù)值的行,True表示是重復(fù)值(行)
參數(shù)
? subset: column label or sequence of labels, optional
可以指定檢測某一列是否有重復(fù)值。默認(rèn)將檢測pandas數(shù)據(jù)中是否有重復(fù)行
? keep: {first, last, False}, default first
first: 對于所有重復(fù)值,標(biāo)記除第一次出現(xiàn)的重復(fù)值,默認(rèn)。
last: 對于所有重復(fù)值,標(biāo)記除最后一次出現(xiàn)的重復(fù)值
False: 標(biāo)記所有重復(fù)值
df = pd.DataFrame({'brand': ['Yum Yum', 'Yum Yum', 'Indomie', 'Indomie', 'Indomie'],'style': ['cup', 'cup', 'cup', 'pack', 'pack'],'rating': [4, 4, 3.5, 15, 5] }) dfbrand 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.duplicated()0 False 1 True 2 False 3 False 4 False dtype: boolpandas.DataFrame.drop_duplicates
drop_duplicates api
- DataFrame.``drop_duplicates(subset=None, keep=‘first’, inplace=False, ignore_index=False)
返回已去重的DataFrame結(jié)構(gòu),默認(rèn)保留第一次出現(xiàn)的行(值)、非原地操作、不為去重后的行添加默認(rèn)索引
參數(shù)
-
subset: column label or sequence of labels, optional
Only consider certain columns for identifying duplicates, by default use all of the columns.
-
keep: {‘first’, ‘last’, False}, default ‘first’
同pandas.DataFrame.duplicated()
-
inplace: bool, default False
Whether to drop duplicates in place or to return a copy.
-
ignore_index: bool, default False
If True, the resulting axis will be labeled 0, 1, …, n - 1.New in version 1.0.0.
Returns
-
DataFrame or None
DataFrame with duplicates removed or None if inplace=True.
pandas.Series.value_counts
value_counts api
- Series.value_counts(normalize=False, sort=True, ascending=False, bins=None, dropna=True)
統(tǒng)計各種值出現(xiàn)的次數(shù),默認(rèn)降序排列,以便將次數(shù)最多的值(除NA)置頂
index = pd.Index([3, 1, 2, 3, 4, np.nan]) index.value_counts()3.0 2 2.0 1 4.0 1 1.0 1 dtype: int64總結(jié)
以上是生活随笔為你收集整理的pandas去重函数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 自定义mysql8.0安装路径
- 下一篇: 如何求解问题--数据结构与算法入门