Pandas中DataFrame數(shù)據(jù)刪除詳情
本文介紹
Pandas
中DataFrame
數(shù)據(jù)刪除,主要使用drop
、del
方式。
# drop函數(shù)的參數(shù)解釋 drop( self, labels=None, # 就是要?jiǎng)h除的行列的標(biāo)簽,用列表給定; axis=0, # axis是指處哪一個(gè)軸,0為行(默認(rèn)),1為列; index=None, # index是指某一行或者多行 columns=None, # columns是指某一列或者多列 level=None, # level是指等級(jí),針對(duì)多重索引的情況; inplace=False, # inplaces是否替換原來(lái)的dataframe; errors="raise", ) axis=0或者 和 index或columns 指定行列只需要使用一組就行
1.根據(jù)默認(rèn)的行列索引操作
示例數(shù)據(jù)
import numpy as np import pandas as pd # 生成隨機(jī)數(shù)組-5行5列 df = pd.DataFrame(np.random.rand(5,5)) print(df)
數(shù)據(jù)展示
0 1 2 3 4 0 0.760489 0.074633 0.788416 0.087612 0.560539 1 0.758450 0.599777 0.384075 0.525483 0.628910 2 0.386808 0.148106 0.742207 0.452627 0.775963 3 0.662909 0.134640 0.186186 0.735429 0.459556 4 0.328694 0.269088 0.331404 0.835388 0.899107
1.1行刪除
[1]刪除單行
# 刪除單行,刪除第2行 df.drop(df.index[1],inplace=True) # inplace=True 原地修改 print(df)
執(zhí)行結(jié)果:
0 1 2 3 4
0 0.605764 0.234973 0.566346 0.598105 0.478153
2 0.383230 0.822174 0.228855 0.743258 0.076701
3 0.875287 0.576668 0.176982 0.341827 0.112582
4 0.205425 0.898544 0.799174 0.000905 0.377990
[2]刪除不連續(xù)多行
# 刪除不連續(xù)多行,刪除第2和第4行 df.drop(df.index[[1,3]],inplace=True) print(df)
執(zhí)行結(jié)果:
0 1 2 3 4
0 0.978612 0.556539 0.781362 0.547527 0.706686
2 0.845822 0.321716 0.444176 0.053915 0.296631
4 0.617735 0.040859 0.129235 0.525116 0.005357
[3]刪除連續(xù)多行
# 刪除連續(xù)多行 df.drop(df.index[1:3],inplace=True) # 開(kāi)區(qū)間,最后一個(gè)索引號(hào)不計(jì)算在內(nèi) print(df)
執(zhí)行結(jié)果:
0 1 2 3 4
0 0.072891 0.926297 0.882265 0.971368 0.567840
3 0.163212 0.546069 0.360990 0.494274 0.065744
4 0.752917 0.242112 0.526675 0.918713 0.320725
1.2列刪除
列的刪除可以使用
del
和drop
兩種方式,del df[1] # 刪除第2列,該種方式為原地刪除,本文具體講解drop函數(shù)刪除。
[1]刪除指定列
df.drop([1,3],axis=1,inplace=True) # 指定軸為列 # df.drop(columns=[1,3],inplace=True) # 直接指定列
執(zhí)行結(jié)果:
0 2 4
0 0.592869 0.123369 0.815126
1 0.127064 0.093994 0.332790
2 0.411560 0.118753 0.143854
3 0.965317 0.267740 0.349927
4 0.688604 0.699658 0.932645
[2]刪除連續(xù)列
df.drop(df.columns[1:3],axis=1,inplace=True) #指定軸 # df.drop(columns=df.columns[1:3],inplace = True) # 指定列 print(df)
執(zhí)行結(jié)果:
0 3 4
0 0.309674 0.974694 0.660285
1 0.677328 0.969440 0.953452
2 0.954114 0.953569 0.959771
3 0.365643 0.417065 0.951372
4 0.733081 0.880914 0.804032
2.根據(jù)自定義的行列索引操作
示例數(shù)據(jù)
df = pd.DataFrame(data=np.random.rand(5,5)) df.index = list('abcde') df.columns = list('一二三四五') print(df)
數(shù)據(jù)展示
一 二 三 四 五 a 0.188495 0.574422 0.530326 0.842489 0.474946 b 0.912522 0.982093 0.964031 0.498638 0.826693 c 0.580789 0.013957 0.515229 0.795052 0.859267 d 0.540641 0.865602 0.305256 0.552566 0.754791 e 0.375407 0.236118 0.129210 0.711744 0.067356
2.1行刪除
[1]刪除單行
df.drop(['b'],inplace=True) print(df)
執(zhí)行結(jié)果:
一 二 三 四 五
a 0.306350 0.622067 0.030573 0.490563 0.009987
c 0.672423 0.071661 0.274529 0.400086 0.263024
d 0.654204 0.809087 0.066099 0.167290 0.534452
e 0.628917 0.232629 0.070167 0.469962 0.957898
[2]刪除多行
df.drop(['b','d'],inplace=True) print(df)
執(zhí)行結(jié)果:
一 二 三 四 五
a 0.391583 0.509862 0.924634 0.466563 0.058414
c 0.802016 0.621347 0.659215 0.575728 0.935811
e 0.223372 0.286116 0.130587 0.113544 0.910859
2.2列刪除
[1]刪除單列
df.drop(['二'],axis=1,inplace=True)# 刪除單列 print(df)
執(zhí)行結(jié)果:
一 三 四 五
a 0.276147 0.797404 0.184472 0.081162
b 0.630190 0.328055 0.428668 0.168491
c 0.979958 0.029032 0.934626 0.106805
d 0.762995 0.003134 0.136252 0.317423
e 0.137211 0.116607 0.367742 0.840080
[2]刪除多列
df.drop(['二','四'],axis=1,inplace=True) # 刪除多列 # df.drop(columns=['二','四'],inplace=True) # 刪除多列 print(df)
執(zhí)行結(jié)果:
一 三 五
a 0.665647 0.709243 0.019711
b 0.920729 0.995913 0.490998
c 0.352816 0.185802 0.406174
d 0.136414 0.563546 0.762806
e 0.259710 0.775422 0.794880
到此這篇關(guān)于Pandas
中DataFrame
數(shù)據(jù)刪除詳情的文章就介紹到這了,更多相關(guān)Pandas
中DataFrame
數(shù)據(jù)刪除 原創(chuàng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Pandas實(shí)現(xiàn)Dataframe的重排和旋轉(zhuǎn)
- Pandas實(shí)現(xiàn)Dataframe的合并
- pandas中DataFrame數(shù)據(jù)合并連接(merge、join、concat)
- 教你漂亮打印Pandas DataFrames和Series
- pandas中DataFrame檢測(cè)重復(fù)值的實(shí)現(xiàn)
- 使用pandas忽略行列索引,縱向拼接多個(gè)dataframe
- Pandas.DataFrame轉(zhuǎn)置的實(shí)現(xiàn)
- Pandas中DataFrame交換列順序的方法實(shí)現(xiàn)
- 詳解pandas中利用DataFrame對(duì)象的.loc[]、.iloc[]方法抽取數(shù)據(jù)
- Pandas中兩個(gè)dataframe的交集和差集的示例代碼
- Pandas DataFrame求差集的示例代碼
- 淺談pandas dataframe對(duì)除數(shù)是零的處理
相關(guān)文章
Python庫(kù)coala代碼分析和自動(dòng)化檢查改進(jìn)工具使用探索
Python的coala工具是一個(gè)強(qiáng)大的代碼分析和自動(dòng)化改進(jìn)工具,它可以幫助開(kāi)發(fā)人員自動(dòng)化地檢查代碼并提供改進(jìn)建議,本文將詳細(xì)介紹coala的功能和用法,并提供豐富的示例代碼來(lái)幫助大家深入了解它2024-01-01python用pd.read_csv()方法來(lái)讀取csv文件的實(shí)現(xiàn)
本文主要介紹了python用pd.read_csv()方法來(lái)讀取csv文件的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06python實(shí)現(xiàn)PID算法及測(cè)試的例子
今天小編就為大家分享一篇python實(shí)現(xiàn)PID算法及測(cè)試的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08python 函數(shù)中的內(nèi)置函數(shù)及用法詳解
這篇文章主要介紹了python 函數(shù)中的內(nèi)置函數(shù) 及用法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07python+selenium實(shí)現(xiàn)自動(dòng)化百度搜索關(guān)鍵詞
在本篇文章里我們給大家分享了一篇關(guān)于python+selenium實(shí)現(xiàn)自動(dòng)化百度搜索關(guān)鍵詞的實(shí)例文章,需要的朋友們可以跟著操作下。2019-06-06