【跟着stackoverflow学Pandas】Delete column from pandas DataFrame-删除列
最近做一个系列博客,跟着stackoverflow学Pandas。
以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序:
https://stackoverflow.com/questions/tagged/pandas?sort=votes&pageSize=15
Delete column from pandas DataFrame - 删除列
stackoverflow 地址:https://stackoverflow.com/questions/13411544/delete-column-from-pandas-dataframe
pandas 官方给出了对列的操作,可以参考:
http://pandas.pydata.org/pandas-docs/stable/dsintro.html#column-selection-addition-deletion
引子
DataFrame删除一个列可以采用
del df['column_name']
但是却不能采用
del df.column_name
首先, 运行del df['column_name'] 时,python内部其实是运行了 df.__delitem__('column_name') 这个函数,实现了对列的删除。
而 del df.column_name 要做的是考虑删除Dataframe的一个属性,是不允许的。
drop
除了上面提到的 del df['column_name'] , 我们还可以采用drop函数。
http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.drop.html
# 按照列名删除
df.drop('column_name', axis=1, inplace=True)
# 根据索引删除
df.drop(df.columns[[0, 1, 3]], axis=1,inplace=True)
# 删除没有的列
df.drop(['col_name_1','col_name_2',...,'col_name_N'],inplace=True,axis=1,errors='ignore')
# errors='ignore', 表示如果要删除的列不在df中,则忽略该错误
inplace 属性
inplace = True 理解为就地修改原有DataFrame, 返回None
df = pd.DataFrame.from_items([('A', [1, 2, 3]), ('B', [4, 5, 6]), ('C', [7,8, 9])], orient='index', columns=['one', 'two', 'three'])
df.drop(df.columns[[0]], axis=1, inplace=False)
two three
A 2 3
B 5 6
C 8 9
In [5]: df
Out[5]:
one two three
A 1 2 3
B 4 5 6
C 7 8 9
df.drop(df.columns[[0]], axis=1, inplace=True)
df
Out[7]:
two three
A 2 3
B 5 6
C 8 9
pop (新添加)
col_df = df.pop(col_names)
【跟着stackoverflow学Pandas】Delete column from pandas DataFrame-删除列的更多相关文章
- 【跟着stackoverflow学Pandas】 - Adding new column to existing DataFrame in Python pandas - Pandas 添加列
最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...
- 【跟着stackoverflow学Pandas】 -Get list from pandas DataFrame column headers - Pandas 获取列名
最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...
- 【跟着stackoverflow学Pandas】Select rows from a DataFrame based on values in a column -pandas 筛选
最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...
- 【跟着stackoverflow学Pandas】add one row in a pandas.DataFrame -DataFrame添加行
最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...
- 【跟着stackoverflow学Pandas】How to iterate over rows in a DataFrame in Pandas-DataFrame按行迭代
最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...
- 【跟着stackoverflow学Pandas】“Large data” work flows using pandas-pandas大数据处理流程
最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...
- 【跟着stackoverflow学Pandas】Renaming columns in pandas-列的重命名
最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...
- 小白学 Python 数据分析(4):Pandas (三)数据结构 DataFrame
在家为国家做贡献太无聊,不如跟我一起学点 Python 人生苦短,我用 Python 前文传送门: 小白学 Python 数据分析(1):数据分析基础 小白学 Python 数据分析(2):Panda ...
- pandas教程1:pandas数据结构入门
pandas是一个用于进行python科学计算的常用库,包含高级的数据结构和精巧的工具,使得在Python中处理数据非常快速和简单.pandas建造在NumPy之上,它使得以NumPy为中心的应用很容 ...
随机推荐
- nginx和php之间是怎样通信的呢?
FastCGI原理 FastCGI是一个运用于Http Server和动态脚本语言间通信的接口,多数流行的Http Server都支持FastCGI,包括Apache.Nginx和lighttpd等. ...
- JS时间和字符串的相互转换 Date+String
1.js字符串转换成时间 1.1方法一:输入的时间格式为yyyy-MM-dd function convertDateFromString(dateString) { if (dateString) ...
- BootStrap实现图片轮播
<div class="container"> <div data-ride="carousel" id="carou ...
- POJ 2051 argus(简单题,堆排序or优先队列)
又是一道数据结构题,使用堆来进行权值调整和排序,每次调整都是o(n)的复杂度,非常高效. 第一眼看题觉得可以用优先队列来做,应该也很简单. 事实上多数优先队列都是通过堆来实现的. 写的时候还是出了一些 ...
- 第十篇:Spark SQL 源码分析之 In-Memory Columnar Storage源码分析之 query
/** Spark SQL源码分析系列文章*/ 前面讲到了Spark SQL In-Memory Columnar Storage的存储结构是基于列存储的. 那么基于以上存储结构,我们查询cache在 ...
- counting the buildings - 第一类斯特灵数
2017-08-10 21:10:08 writer:pprp //TLE #include <iostream> #include <cstdio> #include < ...
- eclipse创建文件package,source folder和folder区别及相互转换
原文:http://blog.csdn.net/u014079773/article/details/66973910 https://www.cnblogs.com/shihaiming/p/735 ...
- js 光标选中 操作
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Vue 及框架响应式系统原理
个人bolg地址 全局概览 Vue运行内部运行机制 总览图: 初始化及挂载 在 new Vue()之后. Vue 会调用 _init 函数进行初始化,也就是这里的 init 过程,它会初始化生命周期. ...
- 全--教程API, gem 'rest-client'(用于发简单请求); 请求测试;
安装:rest-client4400✨ gem install rest-client 一个简单的HTTP和REST client for Ruby. 可以用它来发HTTP请求 基本用法: requi ...