0

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_')

Julia
  • 23
  • 4
  • Welcome to [Stack Overflow.](https://stackoverflow.com/ "Stack Overflow") This is not a code-writing or tutoring service. We can help solve specific, technical problems, not open-ended requests for code or advice. It is very difficult to answer your question without seeing the data that produces your problem. Please read about how to ask a good question and try to post a [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example "Minimal Reproducible Example") so we can better help you. – itprorh66 May 19 '22 at 14:13

1 Answers1

0

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.

ArchAngelPwn
  • 2,891
  • 1
  • 4
  • 17