I would like to find if all the required column names are present in the excel using python. for example:
Header1 Header2 Header3
Val1 Val4 Val6
Val2 val5 Val7
I want to know if header4 is present or not
I use the following:
import pandas as pd
path=C:\Req_file\excel_file
xl = pd.Excelfile(path)
for name in xl.sheet_names:
df = pd.read_excel(xl, name)
my_cols = [Header1, Header2,Header3,Header4]
print(df[my_cols])
It generates a
Keyerror: [header4] not in index
I would like to know is it possible to do with "if" statement. I want to generate an error message on the frame, but I get only in the terminal.
Thanks a lot in advance.
df.columnswill list the names of the column headers: you can test if your column of interest is present:if "my_column" in df.columns:.try:overif:. blogs.msdn.microsoft.com/pythonengineering/2016/06/29/…try: df[required_columns]; except KeyError:. Potentially better is even to just let the KeyError bubble up to the user.try: df[required_columns]; except KeyError:looks correct to me. I like the set solution you posted, but in this instance yourtry:code makes more sense to me. I think you should add it to your answer maybe.