Python-比较值
Python-比较值,python,pandas,Python,Pandas,如何从下一行为NaN复制值,并将标志添加为“(复制)”? 完成- 如果值不相同,如何将第2行和第3行的值与第1行的值进行比较,并将字符串添加为“(不相同)”,从而获得预期的输出 将遮罩保留在变量中,以便以后使用: df = pd.DataFrame({"col1":[np.NaN,"Cat","Cheese"], "col2":["Dog","Che
NaN
复制值,并将标志添加为“(复制)”?
完成-第2行
和第3行
的值与第1行的值进行比较,并将字符串添加为“(不相同)”,从而获得预期的输出
将遮罩保留在变量中,以便以后使用:
df = pd.DataFrame({"col1":[np.NaN,"Cat","Cheese"],
"col2":["Dog","Cheese","Dog"],
"col3":[np.NaN,"Cat","Dog"]})
copy_mask = df.isnull()
df = df.bfill()
same_mask = df!=df.iloc[0]
df[same_mask]+=" (Not same)"
df[copy_mask]+=" (Copy)"
print (df)
col1 col2 col3
0 Cat (Copy) Dog Cat (Copy)
1 Cat Cheese (Not same) Cat
2 Cheese (Not same) Dog Dog (Not same)
包括可以复制粘贴的数据,而不是图像。
df = pd.DataFrame({"col1":[np.NaN,"Cat","Cheese"],
"col2":["Dog","Cheese","Dog"],
"col3":[np.NaN,"Cat","Dog"]})
copy_mask = df.isnull()
df = df.bfill()
same_mask = df!=df.iloc[0]
df[same_mask]+=" (Not same)"
df[copy_mask]+=" (Copy)"
print (df)
col1 col2 col3
0 Cat (Copy) Dog Cat (Copy)
1 Cat Cheese (Not same) Cat
2 Cheese (Not same) Dog Dog (Not same)