Pandas DataFrame drop 删除行列
【摘要】 所属的课程名称及链接[AI基础课程--常用框架工具]环境信息* ModelArts * Notebook - Multi-Engine 2.0 (python3) * JupyterLab - Notebook - Conda-python3 * pandas 0.22.0Pandas DataFrame drop 删除行列import pandas as pdimport...
所属的课程名称及链接
环境信息
- * ModelArts
- * Notebook - Multi-Engine 2.0 (python3)
- * JupyterLab - Notebook - Conda-python3
- * pandas 0.22.0
- * JupyterLab - Notebook - Conda-python3
- * Notebook - Multi-Engine 2.0 (python3)
Pandas DataFrame drop 删除行列
import pandas as pd
import numpy as np
my_df = pd.DataFrame(data=np.arange(50).reshape(10,5), # 10*5的矩阵
index=list("abcdefghij"), # 行索引
columns=list("ABCDE")) # 列索引
print("my_df\n",my_df)
# axis=0 删除行
print("my_df.drop(['a'],axis=0\n",my_df.drop(['a'],axis=0))
# axis=1 删除列
print("my_df.drop(['B','E'],axis=1)\n",my_df.drop(['B','E'],axis=1))
my_df
A B C D E
a 0 1 2 3 4
b 5 6 7 8 9
c 10 11 12 13 14
d 15 16 17 18 19
e 20 21 22 23 24
f 25 26 27 28 29
g 30 31 32 33 34
h 35 36 37 38 39
i 40 41 42 43 44
j 45 46 47 48 49
my_df.drop(['a'],axis=0
A B C D E
b 5 6 7 8 9
c 10 11 12 13 14
d 15 16 17 18 19
e 20 21 22 23 24
f 25 26 27 28 29
g 30 31 32 33 34
h 35 36 37 38 39
i 40 41 42 43 44
j 45 46 47 48 49
my_df.drop(['B','E'],axis=1)
A C D
a 0 2 3
b 5 7 8
c 10 12 13
d 15 17 18
e 20 22 23
f 25 27 28
g 30 32 33
h 35 37 38
i 40 42 43
j 45 47 48
help
help(my_df.drop)
Help on method drop in module pandas.core.generic:
drop(labels=None, axis=0, index=None, columns=None, level=None, inplace=False, errors='raise') method of pandas.core.frame.DataFrame instance
Return new object with labels in requested axis removed.
Parameters
----------
labels : single label or list-like
Index or column labels to drop.
axis : int or axis name
Whether to drop labels from the index (0 / 'index') or
columns (1 / 'columns').
index, columns : single label or list-like
Alternative to specifying `axis` (``labels, axis=1`` is
equivalent to ``columns=labels``).
.. versionadded:: 0.21.0
level : int or level name, default None
For MultiIndex
inplace : bool, default False
If True, do operation inplace and return None.
errors : {'ignore', 'raise'}, default 'raise'
If 'ignore', suppress error and existing labels are dropped.
Returns
-------
dropped : type of caller
Examples
--------
>>> df = pd.DataFrame(np.arange(12).reshape(3,4),
columns=['A', 'B', 'C', 'D'])
>>> df
A B C D
0 0 1 2 3
1 4 5 6 7
2 8 9 10 11
Drop columns
>>> df.drop(['B', 'C'], axis=1)
A D
0 0 3
1 4 7
2 8 11
>>> df.drop(columns=['B', 'C'])
A D
0 0 3
1 4 7
2 8 11
Drop a row by index
>>> df.drop([0, 1])
A B C D
2 8 9 10 11
Notes
-----
Specifying both `labels` and `index` or `columns` will raise a
ValueError.
备注
1. 感谢老师的教学与课件
2. 欢迎各位同学一起来交流学习心得^_^
3. 沙箱实验、认证、论坛和直播,其中包含了许多优质的内容,推荐了解与学习。
2. 欢迎各位同学一起来交流学习心得^_^
3. 沙箱实验、认证、论坛和直播,其中包含了许多优质的内容,推荐了解与学习。
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)