I have a dataframe in Pandas containing planet names and information about each planet. Planet names are stored under the column with the header 'planet'. I want to highlight rows of specific planets, Earth, Saturn, and Mars. For some reason, when I run this script and export as an Excel file, only the first planet in my highlight_planets list is highlighted. In this case, only Earth is highlighted (Saturn and Mars are not highlighted). If I move Saturn into the first position and Earth into the second position, only Saturn rows will be highlighted in the dataframe. How do I highlight all rows where my planet column contains Earth, Saturn, or Mars?
Thanks.
def highlight_sentiment(row):
highlight_planets = ['Earth', 'Saturn', 'Mars']
for m in highlight_planets:
if row['planet'] == m:
return ['background-color: yellow'] * len(row)
else:
return ['background-color: white'] * len(row)
df.style.apply(highlight_sentiment, axis=1)
df = df.style.apply(highlight_sentiment, axis=1)
df.to_excel("df_test.xlsx", index = False)
