I have a pandas DataFrame with multiple columns (columns names are numbers; 1, 2, ...) and I want to copy some of them if they do exist.
For example df1 = df[[1,2,3,4]]
But it might happen that some columns do not exist in df, eg df might only have columns 1, 2, and 4 or columns 1, and 2 etc
In order to check if a list of multiple selected columns exist in pandas DataFrame, use set. issubset . For Example, if set(['Courses','Duration']). issubset(df.
Use isin
with loc
to filter, this will handle non-existent columns:
In [97]:
df = pd.DataFrame(columns=[1,2,4])
df.loc[:,df.columns.isin([1,2,3,4,])]
Out[97]:
Empty DataFrame
Columns: [1, 2, 4]
Index: []
It is simpler to directly calculate the set of common columns and ask for them:
df[df.columns & [1, 2, 3, 4]]
(The &
operator is the (set) intersection operator.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With