Pandas DataFrame DataFrame.drop_duplicates() 函数
    
    Suraj Joshi
    2023年1月30日
    
    Pandas
    Pandas DataFrame
    
- 
          
            pandas.DataFrame.drop_duplicates()的语法
- 
          
            示例代码:使用 Pandas DataFrame.set_index()方法删除重复的行
- 
          
            示例代码设置 subset参数的 PandasDataFrame.set_index()方法
- 
          
            示例代码:设置 keep参数 PandasDataFrame.set_index()方法
- 
          
            示例代码:设置 ignore_index参数的 PandasDataFrame.set_index()方法
 
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 | first、last或False。删除除第一个以外的所有重复数据(keep=first),删除除最后一个以外的所有重复数据(keep=last)或删除所有重复数据(keep=False) | 
| inplace | Boolean.如果为 True,修改调用者的DataFrame。 | 
| ignore_index | 布尔型。如果是 True,则忽略原始DataFrame中的索引,默认值是False,即使用索引。默认值是False,表示使用索引。 | 
返回值
如果 inplace 为 True,则从 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 被修改。
        Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
    
作者: Suraj Joshi
    Suraj Joshi is a backend software engineer at Matrice.ai.
LinkedIn