【发布时间】:2019-03-23 22:46:58
【问题描述】:
美好的一天 SO 社区,
我在尝试逐行突出显示我的 df 中的错误时遇到了问题。
reference_dict = {'jobclass' : ['A','B'], 'Jobs' : ['Teacher','Plumber']}
dict = {'jobclass': ['A','C','A'], 'Jobs': ['Teacher', 'Plumber','Policeman']}
df = pd.DataFrame(data=dict)
def highlight_rows(df):
for i in df.index:
if df.jobclass[i] in reference_dict['jobclass']:
print(df.jobclass[i])
return 'background-color: green'
df.style.apply(highlight_rows, axis = 1)
我收到错误: TypeError: ('字符串索引必须是整数', '发生在索引 0')
我希望得到的是我的 df,其中突出显示了在我的 reference_dict 中找不到的值。
任何帮助将不胜感激..干杯!
编辑:
x = {'jobclass' : ['A','B'], 'Jobs' : ['Teacher','Plumber']}
d = {'jobclass': ['A','C','A'], 'Jobs': ['Teacher', 'Plumber','Policeman']}
df = pd.DataFrame(data=d)
print(df)
def highlight_rows(s):
ret = ["" for i in s.index]
for i in df.index:
if df.jobclass[i] not in x['jobclass']:
ret[s.index.get_loc('Jobs')] = "background-color: yellow"
return ret
df.style.apply(highlight_rows, axis = 1)
试过这个并突出显示整个列而不是我想要的特定行值.. =/
【问题讨论】:
标签: python pandas dataframe highlight