pandas dataframe dataframe.drop-ag捕鱼王app官网

pandas dataframe dataframe.drop_duplicates() 函数

作者:迹忆客 最近更新:2024/04/22 浏览次数:

python pandas dataframe.drop_duplicates() 函数从 dataframe 中删除所有重复的行。


pandas.dataframe.drop_duplicates() 的语法

dataframe.drop_duplicates(subset: union[hashable, sequence[hashable], nonetype]=none,
                          keep: union[str, bool]='first',
                          inplace: bool=false,
                          ignore_index: bool=false)

参数

subset 列标签或标签序列。识别重复时需要考虑的列
keep firstlastfalse。删除除第一个以外的所有重复数据(keep=first),删除除最后一个以外的所有重复数据(keep=last)或删除所有重复数据(keep=false)
inplace boolean.如果为 true,修改调用者的 dataframe
ignore_index 布尔型。如果是 true,则忽略原始 dataframe 中的索引,默认值是 false,即使用索引。默认值是 false,表示使用索引。

返回值

如果 inplacetrue,则从 dataframe 中删除所有重复的行,否则为 none


示例代码:使用 pandas dataframe.set_index() 方法删除重复的行

import pandas as pd
fruit_list = [ ('orange', 34, 'yes' ,'abc') ,
             ('mango', 24, 'no','xyz' ) ,
             ('banana', 14, 'no','bcd' ) ,
            ('orange', 34, 'yes' ,'abc') ]
df = pd.dataframe(fruit_list, 
                  columns = ['name',
                             'price',
                             'in_stock',
                             'supplier'])
print("dataframe:")
print(df)
df_unique=df.drop_duplicates() 
print("dataframe with unique rows:")
print(df_unique)

输出:

dataframe:
     name  price in_stock supplier
0  orange     34      yes      abc
1   mango     24       no      xyz
2  banana     14       no      bcd
3  orange     34      yes      abc
dataframe with unique rows:
     name  price in_stock supplier
0  orange     34      yes      abc
1   mango     24       no      xyz
2  banana     14       no      bcd

原始的 dataframe 的第 1 行和第 4 行是相同的。

你可以通过使用 drop_duplicates() 方法从 dataframe 中删除所有重复的行。


示例代码设置 subset 参数的 pandas dataframe.set_index() 方法

import pandas as pd
fruit_list = [ ('orange', 34, 'yes' ,'abc') ,
             ('mango', 24, 'no','xyz' ) ,
             ('banana', 14, 'no','abc' ) ,
            ('orange', 34, 'yes' ,'abc') ]
df = pd.dataframe(fruit_list, 
                  columns = ['name',
                             'price',
                             'in_stock',
                             'supplier'])
print("dataframe:")
print(df)
df_unique=df.drop_duplicates(subset ="supplier") 
print("dataframe with unique vales of supplier column:")
print(df_unique)

输出:

dataframe:
     name  price in_stock supplier
0  orange     34      yes      abc
1   mango     24       no      xyz
2  banana     14       no      abc
3  orange     34      yes      abc
dataframe with unique vales of supplier column:
     name  price in_stock supplier
0  orange     34      yes      abc
1   mango     24       no      xyz

该方法删除 dataframe 中所有不具有 supplier 列唯一值的行。

在这里,第 1、3 和 4 行的 supplier 列有一个共同的值。因此,第 3 和第 4 行将被从 dataframe 中删除;默认情况下,第一条重复的行不会被删除。


示例代码:设置 keep 参数 pandas dataframe.set_index() 方法

import pandas as pd
fruit_list = [ ('orange', 34, 'yes' ,'abc') ,
             ('mango', 24, 'no','xyz' ) ,
             ('banana', 14, 'no','abc' ) ,
            ('orange', 34, 'yes' ,'abc') ]
df = pd.dataframe(fruit_list, 
                  columns = ['name',
                             'price',
                             'in_stock',
                             'supplier'])
print("dataframe:")
print(df)
df_unique=df.drop_duplicates(subset ="supplier",keep="last") 
print("dataframe with unique vales of supplier column:")
print(df_unique)

输出:

dataframe:
     name  price in_stock supplier
0  orange     34      yes      abc
1   mango     24       no      xyz
2  banana     14       no      abc
3  orange     34      yes      abc
dataframe with unique vales of supplier column:
     name  price in_stock supplier
1   mango     24       no      xyz
3  orange     34      yes      abc

该方法删除 dataframe 中所有在 supplier 列中没有唯一值的行,只保留最后一条重复的行。

在这里,第 1,3,4 行的 supplier 列有一个共同的值。所以第 1 和第 3 行将从 dataframe 中删除。


示例代码:设置 ignore_index 参数的 pandas dataframe.set_index() 方法

import pandas as pd
fruit_list = [ ('orange', 34, 'yes' ,'abc') ,
             ('mango', 24, 'no','xyz' ) ,
             ('banana', 14, 'no','abc' ) ,
            ('orange', 34, 'yes' ,'abc') ]
df = pd.dataframe(fruit_list, 
                  columns = ['name',
                             'price',
                             'in_stock',
                             'supplier'])
print("dataframe:")
print(df)
df.drop_duplicates(subset ="supplier",keep="last",inplace=true,ignore_index=true) 
print("dataframe with unique vales of supplier column:")
print(df)

输出:

dataframe:
     name  price in_stock supplier
0  orange     34      yes      abc
1   mango     24       no      xyz
2  banana     14       no      abc
3  orange     34      yes      abc
dataframe with unique vales of supplier column:
     name  price in_stock supplier
0   mango     24       no      xyz
1  orange     34      yes      abc

这里,由于 ignore_index 被设置为 true,原 dataframe 中的索引被忽略,并为该行设置新的索引。

由于 inplace=true 函数的作用,在调用 ignore_index() 函数后,原 dataframe 被修改。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

pandas read_csv()函数

发布时间:2024/04/24 浏览次数:254 分类:python

pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 dataframe 中。

pandas 追加数据到 csv 中

发布时间:2024/04/24 浏览次数:352 分类:python

本教程演示了如何在追加模式下使用 to_csv()向现有的 csv 文件添加数据。

pandas 多列合并

发布时间:2024/04/24 浏览次数:628 分类:python

本教程介绍了如何在 pandas 中使用 dataframe.merge()方法合并两个 dataframes。

pandas loc vs iloc

发布时间:2024/04/24 浏览次数:837 分类:python

本教程介绍了如何使用 python 中的 loc 和 iloc 从 pandas dataframe 中过滤数据。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便
网站地图