'Problems writing to excel after sorting dataframe
I had the below and everything was working fine:
finalDF = pd.DataFrame.from_dict(compareDictList)
def highlight_rows(row):
if row["Inconsistencies"] == "Y":
return['background-color: yellow'] * len(row)
formatted = finalDF.style.apply(highlight_rows, axis = 1)
formatted.to_excel("FinalReport.xlsx", index=False, columns=['XXXX', 'YYYY', 'ZZZZ'])
But then wanted to first sort the df before writing to excel, so input the single step to sort the df and use that new sorted df with the format styler, but now I'm getting an error when I try to run it all together. The sort part works, but won't highlight and write to excel. Unfortunately I can't share the actual data, here's the code:
finalDF = pd.DataFrame.from_dict(compareDictList)
sortedDF = finalDF.sort_values(by=['XXXX', 'YYYY'])
def highlight_rows(row):
if row["Inconsistencies"] == "Y":
return['background-color: yellow'] * len(row)
formatted = sortedDF.style.apply(highlight_rows, axis = 1)
formatted.to_excel("FinalReport.xlsx", index=False, columns=['XXXX', 'YYYY', 'ZZZZ'])
Solution 1:[1]
OK I finally understood the issue. The problem is when the first row does not meet the criteria, the highlight_rows() returns None which is not the right dimension. To solve this, just return something of the same shape when the condition is False:
def highlight_rows(row):
if row["Inconsistencies"] == "Y":
return ['background-color: yellow'] * len(row)
else:
return ['background-color: None'] * len(row)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
