删除重复数据

2024-04-27 14:27:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个巨大的数据库沿着一个房间的网状流动分布。但问题是网格太小,所以其中一些部分是无用的,使我的计算困难。在我的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]


Tags: 数据代码gtnumpy数据库网格示例尺寸
1条回答
网友
1楼 · 发布于 2024-04-27 14:27:34

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

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

相关问题 更多 >