'Drop or filter columns in pandas dataframe

I would like to filter some columns from my dataframe. The easiest way would be to use the "like" approach (since I have a lot of columns I want to get rid of). I tried df = df.filter(['a', 'b']) like it was advised here: How to delete all columns in DataFrame except certain ones?.

But when I do it like that all my columns disappear...I don't know where my mistake is.

    data = pd.concat([pd.read_table(f, encoding='unicode_escape')
                   .add_suffix(f[+47:-4]) # add prefix  
           for f in file_list], axis=1)
    main_dataframe = pd.DataFrame(data)
    main_dataframe = pd.concat([main_dataframe, data], axis = 1)

main_dataframe = main_dataframe.filter(like='date_')



Solution 1:[1]

Your questions seems a little unclear, but if you are wanting to remove some columns, but not all of them and you have a large set of columns this code will allow you to control which columns you would like to remove from your dataframe easily and dynamically

data = {
    'Column1' : [1, 2, 3, 4, 5],
    'Column2' : [10, 20, 30, 40, 50],
    'Column3' : [100, 200, 300, 400, 500]
}

df = pd.DataFrame(data)
new_column_list = df.columns.drop(['Column3', 'Column2']).tolist()
df = df[new_column_list]
df

This is just an example so you might have to modify it a little, but it should be more than enough to get you started with filtering out some unwanted columns.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1