【问题标题】:Remove duplicate rows from a pandas dataframe: Case Insenstive comparison从熊猫数据框中删除重复行:不区分大小写的比较
【发布时间】:2019-10-01 05:05:09
【问题描述】:

我想根据两列中的值从数据框中删除重复行:Column1 和 Column2

如果dataframe 是:

df = pd.DataFrame({'Column1': ["'cat'", "'toy'", "'cat'"],
                   'Column2': ["'bat'", "'flower'", "'bat'"],
                   'Column3': ["'xyz'", "'abc'", "'lmn'"]})

关于使用:

result_df = df.drop_duplicates(subset=['Column1', 'Column2'], keep='first')
print(result_df)

我明白了:

  Column1   Column2 Column3
0   'cat'     'bat'   'xyz'
1   'toy'  'flower'   'abc'

但dataframe 使用相同的代码(猫和蝙蝠的情况发生了变化)

df = pd.DataFrame({'Column1': ["'Cat'", "'toy'", "'cat'"],
                   'Column2': ["'Bat'", "'flower'", "'bat'"],
                   'Column3': ["'xyz'", "'abc'", "'lmn'"]})

我明白了:

  Column1   Column2 Column3
0   'Cat'     'Bat'   'xyz'
1   'toy'  'flower'   'abc'
2   'cat'     'bat'   'lmn'

预期输出:

  Column1   Column2 Column3
0   'cat'     'bat'   'xyz'
1   'toy'  'flower'   'abc'

如何进行不区分大小写的比较?

【问题讨论】:

  • 似乎没有内置的方法可以做到这一点,通常的做法似乎是先对你的列进行大小写,然后再drop_duplicates
  • @Nullman 但我想保留案例..
  • 在删除重复项之前,我认为您可以将字符串转换为小写。例如。 df['Column1'] = [c1.lower() for c1 in df['Column1']] 为 Column1。对其他列执行相同操作。

标签: python pandas loops dataframe


【解决方案1】:

我想通了。创建新的大写列,然后使用它们删除重复项。完成后,删除大写列。

df = pd.DataFrame({'Column1': ["'Cat'", "'toy'", "'cat'"],
                       'Column2': ["'Bat'", "'flower'", "'bat'"],
                       'Column3': ["'xyz'", "'abc'", "'lmn'"]})

df['Column1_Upper'] = df['Column1'].astype(str).str.upper()
df['Column2_Upper'] = df['Column2'].astype(str).str.upper()

这给出了:

+---+---------+----------+---------+---------------+---------------+
|   | Column1 | Column2  | Column3 | Column1_Upper | Column2_Upper |
+---+---------+----------+---------+---------------+---------------+
| 0 | 'Cat'   | 'Bat'    | 'xyz'   | 'CAT'         | 'BAT'         |
| 1 | 'toy'   | 'flower' | 'abc'   | 'TOY'         | 'FLOWER'      |
| 2 | 'cat'   | 'bat'    | 'lmn'   | 'CAT'         | 'BAT'         |
+---+---------+----------+---------+---------------+---------------+

最后,运行以下命令删除重复的列和创建的列。

result_df = df.drop_duplicates(subset=['Column1_Upper', 'Column2_Upper'], keep='first')
result_df.drop(['Column1_Upper', 'Column2_Upper'], axis=1, inplace=True)
print(result_df)

这给出了:

+-----------------------------+
|   Column1   Column2 Column3 |
+-----------------------------+
| 0   'Cat'     'Bat'   'xyz' |
| 1   'toy'  'flower'   'abc' |
+-----------------------------+

【讨论】:

    【解决方案2】:

    您可以将数据框转换为小写,然后应用您的解决方案。

    您的数据框。

    df = pd.DataFrame({'Column1': ["'Cat'", "'toy'", "'cat'"],
                       'Column2': ["'Bat'", "'flower'", "'bat'"],
                       'Column3': ["'xyz'", "'abc'", "'lmn'"]})
    
    print(df)
    
      Column1   Column2 Column3
    0   'Cat'     'Bat'   'xyz'
    1   'toy'  'flower'   'abc'
    2   'cat'     'bat'   'lmn'
    

    然后应用较低的字符串。

    result_df = df.apply(lambda x: x.astype(str).str.lower()).drop_duplicates(subset=['Column1', 'Column2'], keep='first')
    
    print(result_df)
      Column1   Column2 Column3
    0   'cat'     'bat'   'xyz'
    1   'toy'  'flower'   'abc'
    

    然后过滤 df 为大写。

    df.loc[result_df.index]
    
      Column1   Column2 Column3
    0   'Cat'     'Bat'   'xyz'
    1   'toy'  'flower'   'abc'
    

    【讨论】:

      【解决方案3】:

      首先,使用以下行将所有字符串值转换为小写以使其不区分大小写:

      df[['Column1', 'Column2']] = df[['Column1', 'Column2']].applymap(lambda x: x.lower())

      你会得到如下输出。

      Column1 Column2 Column3 0 'cat' 'bat' 'xyz' 1 'toy' 'flower' 'abc' 2 'cat' 'bat' 'lmn'

      现在应用放置重复功能。

      result_df = df.drop_duplicates(subset=['Column1', 'Column2'], keep='first') print(result_df)

      Column1 Column2 Column3 0 'cat' 'bat' 'xyz' 1 'toy' 'flower' 'abc'

      参考:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.applymap.html

      【讨论】:

        猜你喜欢
        • 2021-09-14
        • 2012-06-03
        • 2020-03-23
        • 1970-01-01
        • 1970-01-01
        • 2017-02-28
        • 2021-12-09
        • 1970-01-01
        • 2016-11-12
        相关资源
        最近更新 更多