-1

I'm reading a csv file that has 7 columns

df = pd.read_csv('DataSet.csv',delimiter=',',usecols=['Wheel','Date','1ex','2ex','3ex','4ex','5ex'])

The problem is that the model I want to train with it, is complaining about the first 2 columns being Strings, so I want to drop them. I first tried not to read the from the beginning with :

df = pd.read_csv('DataSet.csv',delimiter=',',usecols=['1ex','2ex','3ex','4ex','5ex'])

but it only shifted the values of two columns..so I decided to drop them. The problem is that I'm only able to drop the first column 'Date' with

train_df.drop(columns=['Date'], inplace=True)

, train_df is a portion of df uses for testing. How do I go to also drop 'Wheel' column? I tried

train_df.drop(labels=[["Date","Wheel"]], inplace=True)

but i get KeyError: "[('Date', 'Wheel')] not found in axis" so I tried

train_df.drop(columns=[["Date","Wheel"]], index=1, inplace=True)

but I still get the same error. I'm so new to Python I'm out of resources to solve this. As always many thanks.

Vincenzo
  • 5,304
  • 5
  • 38
  • 96
  • Possible duplicate of [Delete column from pandas DataFrame](https://stackoverflow.com/questions/13411544/delete-column-from-pandas-dataframe) – PV8 Oct 07 '19 at 13:26
  • this is well discussed here: https://stackoverflow.com/questions/13411544/delete-column-from-pandas-dataframe – PV8 Oct 07 '19 at 13:26
  • @PV8 well..sorry, I'm very new to it, I actually found that second post you suggested, but I wasn't sure about deleting the columns. – Vincenzo Oct 07 '19 at 13:29

1 Answers1

2

Try:

train_df.drop(columns=["Date","Wheel"], index=1, inplace=True)

See the examples in https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.drop.html

  • Absolutely working thanks! So it was the double `[ ]` that created the problem. I actually did tried it before, but without the `index=1`, the guide I found misled me using the double `[ ]`. Thanks for the link and for not down voting me ;) – Vincenzo Oct 07 '19 at 13:35
  • @Anakin87 I am new to Python, so maybe that's why I am confused; but my question is regarding the `train_df`, not the process of dropping columns. How does the compiler knows this variable is trying to drop columns from the `df`, while the `train_df`was not assigned to `df` before? – RFAI Jun 13 '20 at 07:08
  • In the question, the author explains that `train_df` is a portion of `df`, but he doesn't specify how this portion is defined: a line of code is missing. – Stefano Fiorucci - anakin87 Jun 15 '20 at 06:42