日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Python,Pandas中刪除的方法(1)

系統(tǒng) 2109 0

pandas主要有三個(gè)用來刪除的函數(shù),.drop()、.drop_duplicates()、.dropna()。總結(jié)如下

  • .drop()刪除行、列
  • .drop_duplicates()刪除重復(fù)數(shù)據(jù)
  • .dropna()刪除空值(所在行、列)

為避免篇幅太長,將其分為兩部分,不想看參數(shù)介紹的可以直接看實(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)很詳細(xì)了,這里做一個(gè)總結(jié)和實(shí)例的補(bǔ)充。
drop()功能:從行或列中刪除指定的標(biāo)簽。
方法:通過指定標(biāo)簽名和對應(yīng)的軸,或者直接指定索引名或列名,刪除行或列。使用多索引時(shí),可以通過指定級別刪除不同級別的標(biāo)簽。

下面用人話解釋一下:該函數(shù)可以刪除指定的行或列,指定方式可以用數(shù)字索引也可以用自定義的索引。同時(shí)也可以刪除多索引中不同級別的行或列(多索引后面會(huì)有例子,類似于word中的多級標(biāo)題,drop可以刪除指定級別標(biāo)題下的某一行)

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: 選定所需要?jiǎng)h除的行或列(可以是單獨(dú)的行或列,也可以用數(shù)組表示多個(gè)行或列)
這兩個(gè)參數(shù)與前兩個(gè)有些重復(fù)
當(dāng)使用整數(shù)標(biāo)簽(即0, 1, 2, …)指定labels時(shí),axis=0則刪除行;axis=1則刪除列。
當(dāng)使用自定義標(biāo)簽指定labels時(shí),則直接刪除指定的行或列。

level: 用于多級索引,刪除指定等級的索引行或列,可以填索引等級或者直接填寫索引名稱。

inplace: 是否保留原數(shù)據(jù)。
True則表示不改變原數(shù)據(jù),并將刪除后的結(jié)果重新創(chuàng)建一個(gè)dataframe
False則直接改變原數(shù)據(jù)

  • 代碼實(shí)例
    先創(chuàng)建一個(gè)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默認(rèn)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
#默認(rèn)為False,刪除指令不對原數(shù)據(jù)進(jìn)行,可以將刪除后的結(jié)果保存在新變量中
#當(dāng)改為Ture,刪除指令直接對原數(shù)據(jù)進(jìn)行
df.drop('person1', inplace=True)

            
          

多索引
drop()也可以對多索引進(jìn)行操作,level參數(shù)可以規(guī)定刪除的索引等級。
level指定索引等級,等級與規(guī)定的行或列名稱必須一致,否則雖然不會(huì)報(bào)錯(cuò),但是不會(huì)進(jìn)行任何操作。
我猜這是為了避免一級和二級索引之間有重名誤刪。

            
              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)#等級不匹配,不會(huì)報(bào)錯(cuò),但是沒有進(jìn)行任何操作。
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號(hào)聯(lián)系: 360901061

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

【本文對您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會(huì)非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 澄迈县| 云梦县| 府谷县| 彰化县| 宽甸| 应用必备| 铁力市| 行唐县| 宿松县| 葫芦岛市| 中山市| 绥芬河市| 时尚| 兴安县| 康平县| 南岸区| 安平县| 石门县| 习水县| 万荣县| 沂水县| 曲麻莱县| 沈阳市| 兰西县| 广丰县| 汉源县| 旬阳县| 景洪市| 元朗区| 崇礼县| 绥中县| 子洲县| 城口县| 长岛县| 山东| 鄂州市| 江山市| 东城区| 湖北省| 宁城县| 钟祥市|