我有两个数据框:
source>:27:9: error: no matching member function for call to 'find'
然后我想验证 df_data 中的所有行,并检查(基于 ID 列),如果 df_data_req 中指定的所有列都有值。
我期望的是 df_data 的附加列,它指示“缺少数据”或“正常”,这取决于 df_data_req 中指定的内容。样本数据的输出将是:
ID | col1 | col2 | col3 | 验证 |
---|---|---|---|---|
x | 1 | 5 | 缺失数据 | |
y | 5 | 1 | 好的 | |
x | 2 | 5 | 3 | 好的 |
z | f | 5 | 5 | 好的 |
答案 0 :(得分:3)
这是一种使用 set_index
、df.reindex_like
后跟条件检查的方法:
a = df_data.set_index("ID").replace("",np.nan)
b = df_data_req.set_index("ID").replace("",np.nan)
c = a.fillna(b.reindex_like(a)).eq("X").any(1)
df_data['validation'] = np.where(c,"MissingData","OK")
print(df_data)
ID col1 col2 col3 validation
0 x 1 5 MissingData
1 y 5 1 OK
2 x 2 5 3 OK
3 z f 5 5 OK