![](/img/trans.png)
[英]Highlight a row in a pandas df if that row also appears in another df
[英]How to highlight a row in pandas df if it is in another df?
我試圖突出顯示我的 dataframe 中一列中的任何行(如果它存在於另一列中)。 我努力了:
apics_tonal_features.style.apply(lambda x: ["background: red" if v.isin(blasi_final_features['x']) else "" for v in x], axis = 1)
但是因為我正在比較字符串,所以它給了我錯誤
AttributeError: 'str' object has no attribute 'isin'
這是我正在使用的數據框的一些可重現代碼
apics_tonal_features = pd.DataFrame({'Feature Name': ['Tone', 'Para-linguistic usages of clicks'],
'Feature ID': ['120', '108'],
'Number of Languages': ['74', '64'],
'Number of Variance': ['5', '4'],
'WALS Equivalent': ['WALS 13A', 'WALS 142A']})
blasi_final_features = pd.DataFrame({'x': ['order.of.subject.object.verb', 'Order.of.genitive.and.noun', 'Tone', 'Vowel nasalization']})
請考慮 df。
A B C D
0 1 0 1 1
1 1 2 4 7
2 2 3 5 8
3 3 4 6 9
4 4 5 7 10
5 2 11 2 7
output
A B C D
1: Red 0 1: Red 1: Red
1: Red 2: Red 4: Red 7: Red
2: Red 3: Red 5: Red 8
3: Red 4: Red 6 9
4: Red 5: Red 7: Red 10
2: Red 11 2: Red 7: Red
解釋:
0
和11
不存在於其他列中。6
不存在於其他列中。8
、 9
、 `10z 不存在於其他列中。代碼來實現
df = pd.DataFrame({
"A": [1, 1, 2, 3, 4, 2],
"B": [0, 2, 3, 4, 5, 11],
"C": [1, 4, 5, 6, 7, 2],
"D": [1, 7, 8, 9, 10, 7]
})
df = df.apply(
lambda x: [f"{elem}: Red" if
any([elem in df[column].tolist() for column in if column != x.name])
else elem for elem in x], axis=0)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.