How to access columns in a dataframe having name with special character
Question:
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:
Answers:
If you try to drop a column, the axis should be 1
data.drop(data.columns[1],axis=1)
You can use raw string literals:
df.drop(r"('Date','')", axis=1)
Drop is not in place I’m afraid:
df=df.drop("('Date','')", axis=1)
Alternatively:
df=df.drop(columns="('Date','')")
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:
If you try to drop a column, the axis should be 1
data.drop(data.columns[1],axis=1)
You can use raw string literals:
df.drop(r"('Date','')", axis=1)
Drop is not in place I’m afraid:
df=df.drop("('Date','')", axis=1)
Alternatively:
df=df.drop(columns="('Date','')")