pandas dataframe drop函数介绍
作者:soulsoul_god 时间:2023-07-11 17:19:17
使用drop函数删除dataframe的某列或某行数据:
drop(labels, axis=0, level=None, inplace=False, errors='raise')
-- axis为0时表示删除行,axis为1时表示删除列
常用参数如下:
import pandas as pd
import numpy as np
data = {'Country':['China','US','Japan','EU','UK/Australia', 'UK/Netherland'],
'Number':[100, 150, 120, 90, 30, 2],
'Value': [1, 2, 3, 4, 5, 6],
'label': list('abcdef')}
df = pd.DataFrame(data)
print("df原数据:\n", df, '\n')
out:
df原数据:
Country Number Value label
0 China 100 1 a
1 US 150 2 b
2 Japan 120 3 c
3 EU 90 4 d
4 UK/Australia 30 5 e
5 UK/Netherland 2 6 f
删除单列:
print(df.drop('Country', axis = 1))
out:
Number Value label
0 100 1 a
1 150 2 b
2 120 3 c
3 90 4 d
4 30 5 e
5 2 6 f
删除多列:
print(df.drop(['Country','Number'], axis = 1))
out:
Value label
0 1 a
1 2 b
2 3 c
3 4 d
4 5 e
5 6 f
删除单行:
print(df.drop(labels = 1, axis = 0))
out:
Country Number Value label
0 China 100 1 a
2 Japan 120 3 c
3 EU 90 4 d
4 UK/Australia 30 5 e
5 UK/Netherland 2 6 f
删除多行:
print(df.drop(labels = [1,2], axis = 0))
out:
Country Number Value label
0 China 100 1 a
3 EU 90 4 d
4 UK/Australia 30 5 e
5 UK/Netherland 2 6 f
使用range函数删除连续多行:
print(df.drop(labels = range(1,3), axis = 0))
out:
Country Number Value label
0 China 100 1 a
3 EU 90 4 d
4 UK/Australia 30 5 e
5 UK/Netherland 2 6 f
来源:https://blog.csdn.net/xiadeliang1111/article/details/126846584
标签:pandas,dataframe,drop
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
解释FCKeditor样式的工作原理
2007-10-11 13:34:00
使用python实现学生信息管理系统
2022-11-24 21:28:35
语义、标准和样式
2008-06-05 12:52:00
浅谈python中的错误与异常
2021-11-06 11:51:23
基于Python-Pycharm实现的猴子摘桃小游戏(源代码)
2023-07-07 06:31:22
![](https://img.aspxhome.com/file/2023/5/60705_0s.png)
MYSQL之插入极限分析
2024-01-22 13:35:44
Python守护进程(daemon)代码实例
2021-10-21 21:15:54
python通过re正则表达式切割中英文的操作
2021-11-29 04:41:23
Python如何基于Tesseract实现识别文字功能
2022-11-09 11:52:18
![](https://img.aspxhome.com/file/2023/6/115666_0s.png)
深入分析在Python模块顶层运行的代码引起的一个Bug
2021-06-29 01:26:29
SQLSERVER2008中CTE的Split与CLR的性能比较
2024-01-28 06:56:04
![](https://img.aspxhome.com/file/2023/4/110954_0s.png)
简单聊聊Golang中defer预计算参数
2023-07-22 03:55:09
Python简单I/O操作示例
2021-11-06 04:42:24
bootstrap-table组合表头的实现方法
2024-05-11 09:07:53
![](https://img.aspxhome.com/file/2023/8/127178_0s.png)
基于php权限分配的实现代码
2023-11-14 11:10:49
![](https://img.aspxhome.com/file/2023/3/94503_0s.png)
Python实现计算字符串中出现次数最多的字符示例
2021-03-11 15:59:13
![](https://img.aspxhome.com/file/2023/3/100983_0s.png)
浅谈PyTorch的可重复性问题(如何使实验结果可复现)
2021-07-16 06:34:33
Python实现Socket通信建立TCP反向连接
2023-11-13 04:20:19
![](https://img.aspxhome.com/file/2023/9/86419_0s.jpg)
python3.6实现学生信息管理系统
2021-02-09 20:54:48
定制FileField中的上传文件名称实例
2022-06-07 14:21:05