pandas主要有三個用來刪除的函數(shù),.drop()、.drop_duplicates()、.dropna()。總結(jié)如下
- .drop()刪除行、列
- .drop_duplicates()刪除重復(fù)數(shù)據(jù)
- .dropna()刪除空值(所在行、列)
為避免篇幅太長,將其分為兩部分,不想看參數(shù)介紹的可以直接看實例。
本篇介紹.drop()
官方介紹:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.drop.html#pandas.DataFrame.drop
DataFrame.drop(labels=None, axis=0, index=None, columns=None, level=None, inplace=False, errors='raise')[source]
Drop specified labels from rows or columns.
Remove rows or columns by specifying label names and corresponding axis, or by specifying directly index or column names. When using a multi-index, labels on different levels can be removed by specifying the level.
Parameters:
labels : single label or list-like
Index or column labels to drop.
axis : {0 or ‘index’, 1 or ‘columns’}, default 0
Whether to drop labels from the index (0 or ‘index’) or columns (1 or ‘columns’).
index, columns : single label or list-like
Alternative to specifying axis (labels, axis=1 is equivalent to columns=labels).
level : int or level name, optional
For MultiIndex, level from which the labels will be removed.
inplace : bool, default False
If True, do operation inplace and return None.
errors : {‘ignore’, ‘raise’}, default ‘raise’
If ‘ignore’, suppress error and only existing labels are dropped.
官方的介紹已經(jīng)很詳細了,這里做一個總結(jié)和實例的補充。
drop()功能:從行或列中刪除指定的標簽。
方法:通過指定標簽名和對應(yīng)的軸,或者直接指定索引名或列名,刪除行或列。使用多索引時,可以通過指定級別刪除不同級別的標簽。
下面用人話解釋一下:該函數(shù)可以刪除指定的行或列,指定方式可以用數(shù)字索引也可以用自定義的索引。同時也可以刪除多索引中不同級別的行或列(多索引后面會有例子,類似于word中的多級標題,drop可以刪除指定級別標題下的某一行)
DataFrame.drop(labels=None, axis=0, index=None, columns=None, level=None, inplace=False, errors=‘raise’)
- 參數(shù)介紹:
labels: 指定刪除的行或列
axis: 0
表示行,1表示列。
Whether to drop labels from the index (0 or ‘index’) or columns (1 or ‘columns’).
index, columns:
選定所需要刪除的行或列(可以是單獨的行或列,也可以用數(shù)組表示多個行或列)
這兩個參數(shù)與前兩個有些重復(fù)
當(dāng)使用整數(shù)標簽(即0, 1, 2, …)指定labels時,axis=0則刪除行;axis=1則刪除列。
當(dāng)使用自定義標簽指定labels時,則直接刪除指定的行或列。
level: 用于多級索引,刪除指定等級的索引行或列,可以填索引等級或者直接填寫索引名稱。
inplace:
是否保留原數(shù)據(jù)。
True則表示不改變原數(shù)據(jù),并將刪除后的結(jié)果重新創(chuàng)建一個dataframe
False則直接改變原數(shù)據(jù)
-
代碼實例
先創(chuàng)建一個df
import pandas as pd
df = pd.DataFrame({'age': [10, 11, 12],
'name': ['tim', 'TOm', 'rose'],
'income': [100, 200, 300]},
index = ['person1', 'person2', 'person3'])
print(df)
輸出為:
age name income
person1 10 tim 100
person2 11 TOm 200
person3 12 rose 300
接下來開始刪除
#刪除行(axis默認0,可直接選定指定行)
df.drop('person1')
df.drop(['person1', 'person3'])
print(df)
#刪除列(需要用columns或者axis=1)
df.drop('income', axis=1)#labels和axis組合
df.drop(columns=['age', 'income'])#columns參數(shù)
df.drop(df.columns[[0, 2]], axis=1)
print(df)
#inplace
#默認為False,刪除指令不對原數(shù)據(jù)進行,可以將刪除后的結(jié)果保存在新變量中
#當(dāng)改為Ture,刪除指令直接對原數(shù)據(jù)進行
df.drop('person1', inplace=True)
多索引
drop()也可以對多索引進行操作,level參數(shù)可以規(guī)定刪除的索引等級。
level指定索引等級,等級與規(guī)定的行或列名稱必須一致,否則雖然不會報錯,但是不會進行任何操作。
我猜這是為了避免一級和二級索引之間有重名誤刪。
midx = pd.MultiIndex(levels=[['lama', 'cow', 'falcon'],
['speed', 'weight', 'length']],
codes=[[0, 0, 0, 1, 1, 1, 2, 2, 2],
[0, 1, 2, 0, 1, 2, 0, 1, 2]])
df1 = pd.DataFrame(index=midx, columns=['big', 'small'],
data=[[45, 30], [200, 100], [1.5, 1], [30, 20],
[250, 150], [1.5, 0.8], [320, 250],
[1, 0.8], [0.3,0.2]])
print(df1)
#刪除行或列
df1.drop('cow')
df1.drop('big', axis=1)
df1.drop(index='cow', columns='big')
#level的用法
df1.drop('speed', level=1)#刪除所有的'speed'行
Out[69]:
big small
lama weight 200.0 100.0
length 1.5 1.0
cow weight 250.0 150.0
length 1.5 0.8
falcon weight 1.0 0.8
length 0.3 0.2
df1.drop('speed', level=0)#等級不匹配,不會報錯,但是沒有進行任何操作。
Out[70]:
big small
lama speed 45.0 30.0
weight 200.0 100.0
length 1.5 1.0
cow speed 30.0 20.0
weight 250.0 150.0
length 1.5 0.8
falcon speed 320.0 250.0
weight 1.0 0.8
length 0.3 0.2
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
