pandas主要有三個用來刪除的函數(shù),.drop()、.drop_duplicates()、.dropna()。總結如下
.drop()刪除行、列
.drop_duplicates()刪除重復數(shù)據(jù)
.dropna()刪除空值(所在行、列)
為避免篇幅太長,將其分為兩部分,不想看參數(shù)介紹的可以直接看實例。
本篇介紹.drop_duplicates(), df.dropna
drop_duplicates()的用法
df.drop_duplicates() 則通常用于數(shù)據(jù)去重,即剔除數(shù)據(jù)集中的重復值。官方解釋很詳細,下面做一些解讀。
官方解釋:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.drop_duplicates.html#pandas.DataFrame.drop_duplicates
DataFrame.drop_duplicates(subset=None, keep='first', inplace=False)
Return DataFrame with duplicate rows removed, optionally only considering certain columns.
#返回一個去除了重復行的df,也可以選擇刪除重復列
Parameters:
subset : column label or sequence of labels, optional
subset:可以選擇列標簽或者標簽順序
Only consider certain columns for identifying duplicates, by default use all of the columns
#只刪除某些指定列含有重復值的情況,默認情況下使用所有列
keep : {‘first’, ‘last’, False}, default ‘first’(默認'first')
first : Drop duplicates except for the first occurrence.
#刪除除第一次出現(xiàn)外的重復項。
last : Drop duplicates except for the last occurrence.
#刪除重復項(最后一次發(fā)生的除外)。
False : Drop all duplicates.
#刪除所有的重復項(一個也不留)
inplace : boolean, default False
Whether to drop duplicates in place or to return a copy
#確定是否在原數(shù)據(jù)上進行修改
下面時代碼實例
dic = {'A': [1, 2, 1, 1], 'B': [2, 5, 2, 4],
'C': [3, 5, 3, 10], 'D': [4, 9, 4, 5]}
df = pd.DataFrame(dic, index=['one', 'two', 'three', 'four'])
print(df)
A B C D
one 1 2 3 4
two 2 5 5 9
three 1 2 3 4
four 1 4 10 5
#默認刪除所有每一列都相同的重復行(保留第一行)
df.drop_duplicates()
#刪除指定列重復的某些行(保留第一行)
df.drop_duplicates(subset='A')
#刪除所有每一列都相同的重復行(保留最后一行)
df.drop_duplicates(keep='last')
#刪除所有重復行(一個不留)
df.drop_duplicates(keep=False)
#以上步驟均不再原數(shù)據(jù)上更改
print(df)
#inplace=True時,直接在原數(shù)據(jù)上更改
df.drop_duplicates(inplace=True)
print(df)
dropna()的用法
df.dropna 用于刪除缺少值,即數(shù)據(jù)集中空缺的數(shù)據(jù)列或行。官方給的解釋很詳細,不做贅述,翻譯一下即可。
官方解釋:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.dropna.html#pandas.DataFrame.dropna
DataFrame.dropna(axis=0, how='any', thresh=None, subset=None, inplace=False)
Remove missing values.
去除缺失值
Parameters:
axis : {0 or ‘index’, 1 or ‘columns’}, default 0(默認0)
Determine if rows or columns which contain missing values are removed.
#確定刪除缺失值所在行還是列
0, or ‘index’ : Drop rows which contain missing values.
#0或者'index',刪除缺失值所在行
1, or ‘columns’ : Drop columns which contain missing value.
#1或者'columns',刪除缺失值所在列
how : {‘a(chǎn)ny’, ‘a(chǎn)ll’}, default ‘a(chǎn)ny’(默認'any'
Determine if row or column is removed from DataFrame, when we have at least one NA or all NA.
#當行或列至少有一個缺失值,是否將其刪除
‘a(chǎn)ny’ : If any NA values are present, drop that row or column.
#存在任何缺失值,就將該行或列刪除
‘a(chǎn)ll’ : If all values are NA, drop that row or column.
#只有當該行或列所有值都為缺失值,才刪除該行或列
thresh : int, optional
Require that many non-NA values.
#只保留至少有規(guī)定的(thresh的值)非na值的行。
subset : array-like, optional
Labels along other axis to consider, e.g. if you are dropping rows these would be a list of columns to include.
#沿著其他軸的標記要考慮,例如如果要刪除行,則這些列將包含要包含的列。
inplace : bool, default False
If True, do operation inplace and return None.
如果為True,則直接在原數(shù)據(jù)改動
下面是代碼實例
df = pd.DataFrame({'name': ['Alfred', 'Batman', 'Catwoman', np.nan],
'toy': [np.nan, 'Batmobile', 'Bullwhip', np.nan],
'born': [pd.NaT, pd.Timestamp('1940-06-25'), pd.NaT, np.nan]})
print(df)
#默認刪除所有有缺失值的行
df.dropna()
#刪除所有含有缺失值的列
df.dropna(axis=1)
df.dropna(axis='columns')
#只刪除所有都為缺失值的行
df.dropna(how='all')
#只刪除指定列中含有缺失值的行
df.dropna(subset=['name', 'toy'])
#以上操作均不改動原數(shù)據(jù),inplace=True直接在原數(shù)據(jù)改動
print(df)
df.dropna(inplace=True)
更多文章、技術交流、商務合作、聯(lián)系博主
微信掃碼或搜索:z360901061

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