删除重复数据Python

时间:2019-02-24 18:47:35

标签: python pandas numpy bigdata

我在一个房间里有一个庞大的网状流量分布数据库。但是问题是网格太小,因此它们的某些部分是无用的,并且使我难以进行计算。在我的y尺寸上,每个网格长度为0.00032。我的y维度从0到0.45。如您所知,有很多无用的数据。

我想使每个网格长度等于0.00128,而不是删除不能被0.00128划分的行,该怎么做?

trainProcessed = trainProcessed[trainProcessed[:,4]%0.00128==0]

我已经尝试过这行代码(trainProcessed是我的数据作为一个numpy数组),但是它看起来像0-> 0.00128-> 0.00256-> 0.00512。但是有些行的值为0.00384,也可以除以0.00128。顺便说一下,数组的形状是(888300,8)。

示例数据:

  

X:[0,0,0,0,0.00031999,0.00031999,0.00063999,0.00064,0.00096,0.00096,0.000128,0.000128]

示例输出:

  

X:[0,0,0,0,0.000128,0.000128]

1 个答案:

答案 0 :(得分:2)

对于这种情况和模函数,我将使用小数:

import pandas as pd
from decimal import Decimal
df = pd.DataFrame({'values': [0.00128, 0.00384, 0.367, 0.128, 0.34]})
print(df)

#convert float to str then Decimal and apply the modulo
#keep only rows which are dividable by 0.00128
filter = df.apply(lambda r: Decimal(str(r['values'])) % Decimal('0.00128')  == Decimal('0') ,axis=1)

#if data are smaller you could multiply by power of 10 before modulo
#filter = df.apply(lambda r: Decimal(str(r['values'] * 1000)) % Decimal('0.00128')  == Decimal('0') ,axis=1)
df=df[filter].reset_index(drop=True)

#the line: df=df[~filter].reset_index(drop=True) does the (not filter)
print(df)

初始输出:

    values
0  0.00128
1  0.00384
2  0.36700
3  0.12800
4  0.34000

最终输出

    values
0  0.00128
1  0.00384
2  0.12800