【问题标题】:pandas dataframe drop a row of data with same value熊猫数据框删除一行具有相同值的数据
【发布时间】:2020-02-01 20:06:59
【问题描述】:

我有一个如下所示的数据集,我想删除具有相同值的数据行:

enter image description here

我想我可以检查所有行的值,如果所有行都重复则删除它,或者我可以指定具有特定时间的行(在这种情况下为 12:30),但我不知道如何编码...

我尝试了以下方法并尝试仅删除一行但失败了..

df.drop['2020-01-29 12:30']

有人可以帮我推一下吗?提前致谢!

【问题讨论】:

  • 请不要包含截图,您应该创建一个minimal reproducible example
  • 这能回答你的问题吗? Pandas - Conditional drop duplicates
  • 除此之外,问题不是很清楚。您应该提供示例输入和预期输出。您想通过删除数据来弄乱时间序列似乎很奇怪,但不清楚您认为什么是重复的
  • 对不起,我可以在这里发布数据框吗?我刚刚开始,不知道该怎么问...
  • 嗨,不要只在此处发布您的数据框。友善点:努力发布一个最小的可重现示例供我们查看。你会很快得到一个好的答案。

标签: python pandas


【解决方案1】:

您好,请告诉我这是否适合您,

例如我已经创建了数据框

import pandas as pd

data1={'A':[1,2,3,43],
    'B':[11,22,3,53],
    'C':[21,23,3,433],
    'D':[131,223,3,54]}

df=pd.DataFrame(data1)
df.index.names=['index']
print(df)

数据帧

       A    B   C   D
index               
0      1    11  21  131
1      2    22  23  223
2      3    3   3   3
3      43   53  433 54

ind=df[df['A']+df['B'] == df['C']+df['D']].index # get the index where values are similar. Here i have done the addition of the values from first two columns and same with next two columns, if both sums are equal then get the index.

df.drop(ind,inplace=True)  #drop row (ind=2) and save the dataframe 
print(df)

最终输出

       A    B   C   D
index               
0      1    11  21  131
1      2    22  23  223
3      43   53  433 54

注意:索引 2 行已删除。

【讨论】:

  • 非常感谢这个家伙!我的编码逻辑太糟糕了!创建一个可以整理出相同值的变量,然后将其删除!我开始怀疑你怎么知道我在问什么......
  • @TobyWong 没问题的兄弟。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-12
  • 1970-01-01
  • 2019-05-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-27
相关资源
最近更新 更多