本文是转载文章,仅作为记录,防止优秀文章的丢失
https://blog.csdn.net/qq_18351157/article/details/105785367
请大家前往原创作者博客,多多支持
# age state point
# name
# Alice 24 NY 64
# Bob 42 CA 92
# Charlie 18 CA 70
# Dave 68 TX 70
# Ellen 24 CA 88
# Frank 30 NY 57
方法:其中,axis = 0
表示行,这个参数可有可无,0是默认值
print(df.drop(['Bob', 'Dave', 'Frank'],axis = 0))
# age state point
# name
# Alice 24 NY 64
# Charlie 18 CA 70
# Ellen 24 CA 88
其中,index可有可无,默认
print(df.drop(df.index[[1, 3, 5]]))
# age state point
# name
# Alice 24 NY 64
# Charlie 18 CA 70
# Ellen 24 CA 88
print(df.drop(index=df.index[[1, 3, 5]]))
# age state point
# name
# Alice 24 NY 64
# Charlie 18 CA 70
# Ellen 24 CA 88
体会区别:
df_noindex_sort = df_noindex.sort_values('state')
print(df_noindex_sort)
# name age state point
# 1 Bob 42 CA 92
# 2 Charlie 18 CA 70
# 4 Ellen 24 CA 88
# 0 Alice 24 NY 64
# 5 Frank 30 NY 57
# 3 Dave 68 TX 70
print(df_noindex_sort.index)
# Int64Index([1, 2, 4, 0, 5, 3], dtype='int64')
print(df_noindex_sort.drop([1, 3, 5]))
# name age state point
# 2 Charlie 18 CA 70
# 4 Ellen 24 CA 88
# 0 Alice 24 NY 64
print(df_noindex_sort.drop(df_noindex_sort.index[[1, 3, 5]]))
# name age state point
# 1 Bob 42 CA 92
# 4 Ellen 24 CA 88
# 5 Frank 30 NY 57
体会区别:
print(df.drop(['state', 'point'], axis=1))
# age
# name
# Alice 24
# Bob 42
# Charlie 18
# Dave 68
# Ellen 24
# Frank 30
print(df.drop(columns=['state', 'point']))
# age
# name
# Alice 24
# Bob 42
# Charlie 18
# Dave 68
# Ellen 24
# Frank 30
print(df.columns[[1, 2]])
# Index(['state', 'point'], dtype='object')
print(df.drop(df.columns[[1, 2]], axis=1))
# age
# name
# Alice 24
# Bob 42
# Charlie 18
# Dave 68
# Ellen 24
# Frank 30
print(df.drop(columns=df.columns[[1, 2]]))
# age
# name
# Alice 24
# Bob 42
# Charlie 18
# Dave 68
# Ellen 24
# Frank 30
print(df.drop(index=['Bob', 'Dave', 'Frank'],
columns=['state', 'point']))
# age
# name
# Alice 24
# Charlie 18
# Ellen 24
print(df.drop(index=df.index[[1, 3, 5]],
columns=df.columns[[1, 2]]))
# age
# name
# Alice 24
# Charlie 18
# Ellen 24
df_org = df.copy()
df_org.drop(index=['Bob', 'Dave', 'Frank'],
columns=['state', 'point'], inplace=True)
print(df_org)
# age
# name
# Alice 24
# Charlie 18
# Ellen 24