1

I have a dataframe with column names which contains special characters like

Date    
('Date','') 
('Max_Bid_Price', 'ALBAN')  

The problem is when i am trying to drop ('Date','') columns it is throwing error.

I tried

df["('Date','')"]   # not able to find the label
df["\(\'Date\'\,\'\'\)"] # not able to find the label

but when i tried

data.columns[1] # i got the column value as output
data.drop(data.columns[1],axis=0) # but it still throws an error: "labels [('Date', '')] not contained in axis"

Can anyone help me how to access those columns with name(since i have to do operations on it) and also drop those columns:

aynber
  • 22,380
  • 8
  • 50
  • 63
  • df['(\'Date\',\'\')']. Does that work? – teoeme139 Mar 13 '20 at 19:53
  • Please provide a [mcve]. Why do the names end up like that in the first place? According to what you wrote, that first attempt should work. The second snippet fails because of the incorrect axis parameter, you should read the docs. – AMC Mar 14 '20 at 02:04

3 Answers3

0

If you try to drop a column, the axis should be 1

data.drop(data.columns[1],axis=1)
tianlinhe
  • 991
  • 1
  • 6
  • 15
0

You can use raw string literals:

df.drop(r"('Date','')", axis=1)
AmyChodorowski
  • 392
  • 2
  • 14
0

Drop is not in place I'm afraid:

df=df.drop("('Date','')", axis=1)

Alternatively:

df=df.drop(columns="('Date','')")
Grzegorz Skibinski
  • 12,624
  • 2
  • 11
  • 34