An Excel spreadsheet looked like below.
With Pandas, I want to remove the columns “Project C” and “Project E”, and all rows with value “XX” in Columns “Project A” and “Project D”.
import pandas as pd import numpy as np work_file = "C:\\test.xlsx" df = pd.read_excel(work_file, sheetname = "Sheet1", index_col = 0) column_list_to_remove = [“Project C", “Project E"] results1 = df.drop(column_list_to_remove, axis=1) writer = pd.ExcelWriter("C:\\test new.xlsx") pd.formats.format.header_style = None results1.to_excel(writer,'Sheet1') writer.save() Above work well in removing the columns.
I try added on this line to remove rows with value “XX” in “Project A”, and failed. Without hope to remove those in “Project D”.
results1 = results1[results1."Project A" != "XX"] # SyntaxError: invalid syntax How can I remove those rows? Thank you.
