pythonpandasdataframe

Select columns from dataframe on condition they exist


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


Solution

  • 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: []