Delete column from pandas DataFrame using del df.column

When deleting a column in a DataFrame I use:

del df['column_name']

And this works great. Why can't I use the following?

del df.column_name

As you can access the column/Series as df.column_name , I expect this to work.


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.


The best way to do this in pandas is to use drop :

df = df.drop('column_name', 1)

where 1 is the axis number ( 0 for rows and 1 for columns.)

To delete the column without having to reassign df you can do:

df.drop('column_name', axis=1, inplace=True)

Finally, to drop by column number instead of by column label, try this to delete, eg the 1st, 2nd and 4th columns:

df.drop(df.columns[[0, 1, 3]], axis=1)  # df.columns is zero-based pd.Index 

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)
链接地址: http://www.djcxy.com/p/86460.html

上一篇: 运行单独的Python进程避免GIL吗?

下一篇: 使用del df.column从pandas DataFrame中删除列