1 回答

TA贡献1848条经验 获得超6个赞
您可以使用 为您的第一个条件构造布尔掩码np.subtract.outer。fillna(0)由于所有正整数都大于 0,因此通过处理第二个空条件。
# extract integers from columns
ints = df.columns[1:].str.rsplit('_', n=1).str[-1].astype(int)
# perform elementwise comparison after replacing null amounts with 0
bool_arr = np.subtract.outer(ints, df['amount'].fillna(0)).T
# apply Boolean mask to selected columns
df.iloc[:, 1:] = df.iloc[:, 1:].mask(bool_arr > 0)
print(df)
amount property_1 property_2 property_3 property_4
0 2.0 7.0 2.0 NaN NaN
1 NaN NaN NaN NaN NaN
2 3.0 9.0 1.0 5.0 NaN
3 1.0 8.0 NaN NaN NaN
添加回答
举报