黄色网页视频 I 影音先锋日日狠狠久久 I 秋霞午夜毛片 I 秋霞一二三区 I 国产成人片无码视频 I 国产 精品 自在自线 I av免费观看网站 I 日本精品久久久久中文字幕5 I 91看视频 I 看全色黄大色黄女片18 I 精品不卡一区 I 亚洲最新精品 I 欧美 激情 在线 I 人妻少妇精品久久 I 国产99视频精品免费专区 I 欧美影院 I 欧美精品在欧美一区二区少妇 I av大片网站 I 国产精品黄色片 I 888久久 I 狠狠干最新 I 看看黄色一级片 I 黄色精品久久 I 三级av在线 I 69色综合 I 国产日韩欧美91 I 亚洲精品偷拍 I 激情小说亚洲图片 I 久久国产视频精品 I 国产综合精品一区二区三区 I 色婷婷国产 I 最新成人av在线 I 国产私拍精品 I 日韩成人影音 I 日日夜夜天天综合

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

系統 2323 0

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

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

為避免篇幅太長,將其分為兩部分,不想看參數介紹的可以直接看實例。
本篇介紹.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.

            
          

官方的介紹已經很詳細了,這里做一個總結和實例的補充。
drop()功能:從行或列中刪除指定的標簽。
方法:通過指定標簽名和對應的軸,或者直接指定索引名或列名,刪除行或列。使用多索引時,可以通過指定級別刪除不同級別的標簽。

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

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

  • 參數介紹:

labels: 指定刪除的行或列

axis: 0 表示行,1表示列。
Whether to drop labels from the index (0 or ‘index’) or columns (1 or ‘columns’).

index, columns: 選定所需要刪除的行或列(可以是單獨的行或列,也可以用數組表示多個行或列)
這兩個參數與前兩個有些重復
當使用整數標簽(即0, 1, 2, …)指定labels時,axis=0則刪除行;axis=1則刪除列。
當使用自定義標簽指定labels時,則直接刪除指定的行或列。

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

inplace: 是否保留原數據。
True則表示不改變原數據,并將刪除后的結果重新創建一個dataframe
False則直接改變原數據

  • 代碼實例
    先創建一個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參數
df.drop(df.columns[[0, 2]], axis=1)
print(df)

#inplace
#默認為False,刪除指令不對原數據進行,可以將刪除后的結果保存在新變量中
#當改為Ture,刪除指令直接對原數據進行
df.drop('person1', inplace=True)

            
          

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

            
              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

            
          

更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

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

發表我的評論
最新評論 總共0條評論