我将一个二进制字符串变量转换为 0 和 1,但不知何故我最终得到了 0.5289256 的实例。我试图用网上找到的几种方法删除这一行,但它仍然存在:
OG_df_dropped = OG_df.query("type != 0.528926 ")
unique_values = OG_df_dropped ['type'].unique()
print(unique_values)
[0. 0.52892562 1.
]
我也尝试过这个:
i = OG_df[((OG_df.type == 0.528926 ) )].index
print(i)
OG_df.drop(i)
同样的结果
我错过了什么或做错了什么?
我在 Jupyter 中尝试过,效果很好。如果它不起作用,请告诉我,我怀疑与浮点精度有关。我可以尝试其他方法来帮助!!
import pandas as pd
import numpy as np
data = {
'type': [0.0, 0.5289256, 1.0, 0.5, 0.52892562]
}
OG_df = pd.DataFrame(data)
OG_df
Jupyter Output:
type
0 0.000000
1 0.528926
2 1.000000
3 0.500000
4 0.528926
OG_df_dropped = OG_df[np.abs(OG_df['type'] - 0.5289256) > 1e-7]
unique_values = OG_df_dropped['type'].unique()
print(unique_values)
[0. 1. 0.5]