0

I have tried a number of ways to drop multiple columns in a csv using read_csv and then save down the new csv (with the columns deleted), but none of them work. Here is my latest:

import pandas as pd
df = pd.read_csv('TradedInstrument_20230331_real.csv',  sep='\t', encoding='unicode_escape')
df.drop(columns='stopTrdgAllowedInOpnFlag')
df.drop(columns='stopTrdgsAllowableInTrdg')
df.drop(columns='preTradeBlockThresholdChf')
df.drop(columns='billingSegmentCode')
df.drop(columns='preTradeBlockThresholdChf')
df.drop(columns='billingSegmentDesc').to_csv('output.csv', index=False)

This only removes the billingSegmentDesc.

Patrick_Chong
  • 434
  • 2
  • 12
  • what is your initial `df.columns` ? – RomanPerekhrest Apr 04 '23 at 15:29
  • Ahh let me include it in the question. – Patrick_Chong Apr 04 '23 at 15:29
  • 1
    [`drop`](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.drop.html) is not inplace by default. Add `inplace=True`. – BigBen Apr 04 '23 at 15:30
  • Does this answer your question? [Delete a column from a Pandas DataFrame](https://stackoverflow.com/questions/13411544/delete-a-column-from-a-pandas-dataframe) – BigBen Apr 04 '23 at 15:31
  • Ahh no wonder. One sec @BigBen it does answer the deleting multiple columns, but not sure about when I save it down. Let me try it, and if it does I'll close the Q. – Patrick_Chong Apr 04 '23 at 15:32
  • Or if you don't want to use `inplace`, then `cols = ['stopTrdgAllowedInOpnFlag', 'stopTrdgsAllowableInTrdg', ...]`, then `df.drop(columns=cols).to_csv(...)`. Either way, the linked duplicate covers both `inplace` and dropping multiple columns. – BigBen Apr 04 '23 at 15:34
  • Amazing, thanks Ben. That's so helpful – Patrick_Chong Apr 04 '23 at 15:34

1 Answers1

0

Answer was stated in the comments by BigBen, but like they said, df.drop is not an inplace function. You need to do df = df.drop(columns=colname) or df.drop(columns=colname, inplace=True.
That being said, you can also drop many at once by including just a list.

cols_to_drop = ['colname1, colname2']
df = df.drop(columns=cols_to_drop)
Weezy.F
  • 454
  • 5
  • 10