Reputation: 1
So what I'm trying to do is, based on content in column, set color for whole row (view image for context)
Example: If data in column Status is "Finished", then the color for that row is blue; for "Reading" the color is green etc.
My code:
import pandas as pd
df = pd.read_csv("data.txt", delimiter='|')
def colored_text():
if df.loc[df['Status'] == 'Reading']:
return 'color: green'
else:
return 'color: yellow'
df.style.applymap(colored_text)
print(df)
Output of this is only white-colored table.
I've also tried df.style.apply(colored_text)
with the same result.
At that point I was lost, so I tried df.style.set_properties(color="green")
instead of .apply(colored_text)
and again, the output was the same white-colored table.
So now I'm not even sure if code in def colored_text():
is correct or not
Upvotes: 0
Views: 2280
Reputation: 4543
Use:
import pandas as pd
df = pd.DataFrame({'status': ['Reading', 'Writing', 'Reading'], 'other col': range(3)})
n = len(df.columns)
df.style.apply(lambda x: ["background-color: red"]*n if x['status']== 'Reading' else ["background-color: white"]*n, axis = 1)
Output:
Upvotes: 1