I'm trying to find out whether a specific column exists in my DataFrame columns or not, but I have some problems.
What I do: Using boolean operation "not in" (I've tried any(), all(), "in") to find the specific column header and it seems it's not working properly!
let's say my DataFrame column headers are:
df.columns = ['El-array', 'a', 'b', 'm', 'n', 'Rho', 'dev', 'ip', 'sp', 'vp', 'i',
'M1', 'M2', 'M3', 'M4', 'M5', 'M6', 'M7', 'M8', 'M9', 'M10', 'M11',
'M12', 'M13', 'M14', 'M15', 'M16', 'M17', 'M18', 'M19', 'M20', 'TM1',
'TM2', 'resist', 'DC_slope']
and I'm trying to see if all of 'M1', 'M2', ... 'M20' and 'TM1' are there. If one or more is missing code will not work.
So I say:
if any(['M1','M2','M3','M4','M5','M6','M7','M8','M9','M10','M11',
'M12','M13','M14','M15','M16','M17','M18','M19','M20', 'TM1']) not in df.columns:
print('Incomplete dataset')
Now, let's say df has all the asked columns headers, the if statement still shows the "Incomplete dataset' message!! I have tried "all() not in" too but same result!! I also have tried:
if 'M1' and 'M2' and ... and 'M20' and 'TM1' in df.columns:
"Do this"
elif:
print('Incomplete dataset')
or
if 'M1' or 'M2' or ... or 'M20' and 'TM1' not in df.columns:
print('Incomplete dataset')
elif:
"Do this"
Still prints incomplete dataset!!
Now for a truly incomplete dataset I get the same results too!!