Pandas df.drop_duplicates() 对多个相同的行没有影响

问题描述 投票:0回答:1

当我在 python 中运行 Pandas df.drop_duplicates() 时,重复的行不会消失。 它的行为就像三个3542.87是不同的值(事实上它们是,请阅读我的文章的结尾)。

我的数据片段:

print(df.iloc[234:237,:])
    Date        Libellé                     Montant     solde
291 04/11/2009  CHEQUE N°9667007                -65.00      5030.93
2   05/11/2009  DEBIT CARTE BANCAIRE DIFFERE            -1488.06    3542.87
131 05/11/2009  DEBIT CARTE BANCAIRE DIFFERE            -1488.06    3542.87
290 05/11/2009  DEBIT CARTE BANCAIRE DIFFERE            -1488.06    3542.87
1   05/11/2009  VIREMENT SUR PEA 3E350018328051 VOTRE OPERATI...-25.00      3517.87

我的数据框有不寻常的数据类型:

df.info()
<class 'pandas.core.frame.DataFrame'>
Index: 9404 entries, 111 to 8814
Data columns (total 4 columns):
 #   Column   Non-Null Count  Dtype  
---  ------   --------------  -----  
 0   Date     9404 non-null   object 
 1   Libellé  9404 non-null   object 
 2   Montant  9404 non-null   float64
 3   solde    9404 non-null   float64
dtypes: float64(2), object(2)
memory usage: 367.3+ KB

“Date”是 datetime.date 对象,“Libellé”是字符串。

如果我尝试之后

df.groupby(['Date', 'Libellé', 'Montant'])['solde'].nunique()

在聚合行上

(05/11/2009, DEBIT CARTE BANCAIRE DIFFERE, -1488.06)
,它返回 3 而不是 1。

您知道这是如何实现的以及如何清理我的 solde 值吗?

谢谢。

我尝试使用

.astype(float)
强制采用浮动格式 并且:
all4.iloc[233:238,:].groupby(['Date', 'Libellé', 'Montant'])['solde'].apply(list)
出:

Date        Libellé                                                              Montant 
2009-11-04  CHEQUE N°9667007                                                     -65.00                                             [5030.93]
2009-11-05  DEBIT CARTE BANCAIRE DIFFERE                                         -1488.06    [3542.87, 3542.8700000000003, 3542.870000000001]
            VIREMENT SUR PEA  3E350018328051 VOTRE OPERATION DU    04/11/2009    -25.00                                             [3517.87]
Name: solde, dtype: object

现在的问题是如何清理这些数据并避免这种错误 未来。

group-by format drop-duplicates
1个回答
0
投票
df['solde'] = df['solde'].round(6)

解决了我的问题,但是数据是从 .csv、.xls 和 .xlsx 文件中提取的,也许它可以解释错误的导入:

[3542.87, 3542.8700000000003, 3542.870000000001]
希望这能帮助面临这个棘手问题的人。

© www.soinside.com 2019 - 2024. All rights reserved.