【发布时间】:2020-05-09 22:30:14
【问题描述】:
我从原始 Dataframe 中提取了一个数据子集,看起来像Test Data Frame
并且我要求根据阈值替换数据集值;对于那些小于阈值 = 0.018990814050501657 的条目将等于 0,其余条目将为 1。我尝试了以下代码:
for i in range(0,3):
for col in new_data1:
if math.isnan(new_data1[col][i])==False:
if new_data1[col][i]<threshold:
r_1=new_data1[col].replace(new_data1[col][i],0)
print(r_1)
else:
r_2=new_data1[col].replace(new_data1[col][i],1)
print(r_2)
但我得到了下一个输出:
我得到的结果或多或少符合我的预期,除了最后两列我没有得到一列作为输出,就好像我得到第一列一样
【问题讨论】:
-
使用
np.where(new_data1[col][i]<threshold, 0, 1)。
标签: pandas dataframe filter replace python-3.7