When deleting a column in a DataFrame I use:
del df[\'column_name\']
And this works great. Why can\'t I use the following?
As you've guessed, the right syntax is
del df['column_name']
It's difficult to make del df.column_name
work simply as the result of syntactic limitations in Python. del df[name]
gets translated to df.__delitem__(name)
under the covers by Python.
Deleting a column using iloc
function of dataframe
and slicing
, when we have a typical column name with unwanted values.
df = df.iloc[:,1:] # removing an unnamed index column
Here 0
is the default row and 1
is 1st column so ,1
where starts and stepping is taking default values, hence :,1:
is our parameter for deleting the first column.
The dot syntax works in JavaScript, but not in Python.
del df['column_name']
del df['column_name']
or del df.column_name
Use:
columns = ['Col1', 'Col2', ...]
df.drop(columns, inplace=True, axis=1)
This will delete one or more columns in-place. Note that inplace=True
was added in pandas v0.13 and won't work on older versions. You'd have to assign the result back in that case:
df = df.drop(columns, axis=1)
from version 0.16.1 you can do
df.drop(['column_name'], axis = 1, inplace = True, errors = 'ignore')
In pandas 0.16.1+ you can drop columns only if they exist per the solution posted by @eiTanLaVi. Prior to that version, you can achieve the same result via a conditional list comprehension:
df.drop([col for col in ['col_name_1','col_name_2',...,'col_name_N'] if col in df],
axis=1, inplace=True)