Pandas知识点-drop和drop_duplicates最全总结

drop()参数和用法介绍

drop(labels=None, axis=0, index=None, columns=None, level=None, inplace=False, errors=‘raise’):

labels: 指定要删除的行索引或列名,参数传入方式为字符串或list-like。如果指定的是列名,要配合将axis参数设置为1或columns。axis: 设置删除行还是删除列,0或index表示删除行,1或columns表示删除列,默认值为0。index: 设置要删除的行,相当于设置labels且axis为0或index。columns: 设置要删除的列,相当于设置labels且axis为1或columns。level: 如果索引是多重索引,指定按多重索引中的哪个等级的索引删除,可以传入多重索引的下标或名称。inplace: 设置是否在DataFrame本身删除数据,默认为False,在DataFrame的副本里删除数据,返回删除数据后的DataFrame。如果设置为True,则在调用drop()的DataFrame本身执行删除,返回值为None。errors: 设置是否抛出错误,可以设置的值有{‘ignore’, ‘raise’},默认raise,表示抛出错误。ignore表示忽略错误,跳过传入的错误索引名或列名,正确的索引名或列名不受影响,正常执行删除。

drop()基本使用

# coding=utf-8

import pandas as pd

df1 = pd.DataFrame(

{'A': ['a0', 'a1', 'a2'], 'B': ['b0', 'b1', 'b2'], 'C': ['c0', 'c1', 'c2']},

index=['one', 'two', 'three']

)

print(df1)

Output:

A B C

one a0 b0 c0

two a1 b1 c1

three a2 b2 c2

先创建一个DataFrame,如df1。

print('-'*20, '\n', df1.drop(labels='A', axis=1), sep='')

Output:

--------------------

B C

one b0 c0

two b1 c1

three b2 c2

使用labels和axis参数配合删除列,如果需要删除多列,用列表给labels传参。

print('-'*20, '\n', df1.drop(index='one'), sep='')

print('-'*20, '\n', df1.drop(columns='A'), sep='')

Output:

--------------------

A B C

two a1 b1 c1

three a2 b2 c2

--------------------

B C

one b0 c0

two b1 c1

three b2 c2

使用index和columns可以分别删除行和列,多行多列用列表传入。也可以同时设置index和columns,同时删除行和列。

index和columns中已经隐含了axis的信息,因此不用设置axis。

df1.drop(index='one', inplace=True)

df1.drop(columns='A', inplace=True)

print('-'*20, '\n', df1, sep='')

Output:

--------------------

B C

two b1 c1

three b2 c2

inplace参数设置为True,在DataFrame本身执行行列删除操作,此时返回值为None。

print('-'*20, '\n', df1.drop(columns=['C', 'D'], errors='ignore'), sep='')

Output:

--------------------

B

two b1

three b2

errors参数设置为ignore,可以忽略错误。如df1中没有D列,上面的代码不会报错,会忽略传入的D,正常删除C列。

drop()多重索引使用

df2 = pd.DataFrame(

{'A': ['a0', 'a1', 'a2'], 'B': ['b0', 'b1', 'b2'], 'C': ['c0', 'c1', 'c2']},

index=[['one', 'two', 'three'], [1, 2, 3]]

)

print(df2)

Output:

A B C

one 1 a0 b0 c0

two 2 a1 b1 c1

three 3 a2 b2 c2

新创建一个DataFrame,如df2。

print('-'*20, '\n', df2.drop(labels='two', level=0), sep='')

print('-'*20, '\n', df2.drop(labels=2, level=1), sep='')

Output:

--------------------

A B C

one 1 a0 b0 c0

three 3 a2 b2 c2

--------------------

A B C

one 1 a0 b0 c0

three 3 a2 b2 c2

level参数用于指定多重索引中按哪一级索引删除。如删除上面df2中的第二行,多重索引为[‘two’, 2],指定level为0时,删除索引’two’,指定level为1时,删除索引2,结果相同。

drop_duplicates()参数和用法介绍

drop_duplicates(subset=None, keep=‘first’, inplace=False, ignore_index=False):

subset: 设置根据列的子集来判断重复值,默认根据DataFrame的所有列来判断重复值,即所有列的数据都相同时,才算重复值。如果指定了子集,则只要子集的这些列的数据都相同,就算重复值。keep: 设置保留重复值中的哪一个,可以设置的值有{‘first’, ‘last’, False},默认first,如果有重复值,则保留第一个。设置为last,则保留重复值中的最后一个。设置为False,则删除所有的重复值,一个也不保留。inplace: 同drop()。ignore_index: 设置是否忽略行索引,默认False,去重后的结果的行索引保持原索引不变。如果设置为True,则结果的行索引被重置为0开始的自然数。

drop_duplicates()基本使用

df3 = pd.DataFrame(

{'A': ['a0', 'a1', 'a1', 'a2', 'a2'],

'B': ['b0', 'b1', 'b1', 'b2', 'b2'],

'C': ['c0', 'c1', 'c1', 'c2', 'c3']},

index=['one', 'two', 'three', 'four', 'five']

)

print(df3)

Output:

A B C

one a0 b0 c0

two a1 b1 c1

three a1 b1 c1

four a2 b2 c2

five a2 b2 c3

新创建一个DataFrame,如df3。

print('-'*20, '\n', df3.drop_duplicates(), sep='')

Output:

--------------------

A B C

one a0 b0 c0

two a1 b1 c1

four a2 b2 c2

five a2 b2 c3

默认删除重复值的方式,所有列的数据都相同时判定为重复,保留第一个。

print('-'*20, '\n', df3.drop_duplicates(subset=['A', 'B']), sep='')

Output:

--------------------

A B C

one a0 b0 c0

two a1 b1 c1

four a2 b2 c2

设置subset参数后,只要subset中的列数据相同,就判定为重复值。

print('-'*20, '\n', df3.drop_duplicates(keep='last'), sep='')

print('-'*20, '\n', df3.drop_duplicates(keep=False), sep='')

Output:

--------------------

A B C

one a0 b0 c0

three a1 b1 c1

four a2 b2 c2

five a2 b2 c3

--------------------

A B C

one a0 b0 c0

four a2 b2 c2

five a2 b2 c3

keep参数设置为last,保留重复值中的最后一个。设置为False,则全部都删除,一个也不保留。

print('-'*20, '\n', df3.drop_duplicates(ignore_index=True), sep='')

Output:

--------------------

A B C

0 a0 b0 c0

1 a1 b1 c1

2 a2 b2 c2

3 a2 b2 c3

ignore_index设置为True后,结果的行索引被重置为0开始的自然数。

参考文档: [1] pandas中文网:https://www.pypandas.cn/docs/

相关阅读:

Pandas知识点-reset_index,reindex,reindex_like,你分得清吗?

欢迎 点赞 收藏⭐ 评论 关注❤ 如有错误敬请指正!

☟ 学Python,点击下方名片关注我。☟

相关链接

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: