从 Pandas DataFrame 中删除一列 [英] Delete a column from a Pandas DataFrame

查看:73
本文介绍了从 Pandas DataFrame 中删除一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

删除 DataFrame 中的列时,我使用:

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

因为可以以 df.column_name 的形式访问列/系列,所以我希望它可以工作.

Since it is possible to access the column/Series as df.column_name, I expected this to work.

推荐答案

如您所料,正确的语法是

As you've guessed, the right syntax is

del df['column_name']

由于 Python 中的语法限制,很难让 del df.column_name 正常工作.del df[name] 在 Python 的掩护下被转换为 df.__delitem__(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.

这篇关于从 Pandas DataFrame 中删除一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆