Pandas中DataFrame数据删除详情

目录

  • 1.根据默认的行列索引操作
    • 1.1行删除
    • 1.2列删除
  • 2.根据自定义的行列索引操作
    • 2.1行删除
    • 2.2列删除
本文介绍PandasDataFrame数据删除,主要使用dropdel方式。
# drop函数的参数解释drop(self,labels=None, # 就是要删除的行列的标签,用列表给定; axis=0, # axis是指处哪一个轴,0为行(默认),1为列; index=None, # index是指某一行或者多行columns=None, # columns是指某一列或者多列level=None, # level是指等级,针对多重索引的情况; inplace=False, # inplaces是否替换原来的dataframe; errors="raise",)axis=0或者 和 index或columns 指定行列只需要使用一组就行


1.根据默认的行列索引操作 示例数据
import numpy as npimport pandas as pd# 生成随机数组-5行5列df = pd.DataFrame(np.random.rand(5,5))print(df)

数据展示
0123400.7604890.0746330.7884160.0876120.56053910.7584500.5997770.3840750.5254830.62891020.3868080.1481060.7422070.4526270.77596330.6629090.1346400.1861860.7354290.45955640.3286940.2690880.3314040.8353880.899107


1.1行删除
[1]删除单行
# 删除单行,删除第2行df.drop(df.index[1],inplace=True) # inplace=True 原地修改print(df)

执行结果:
01234
00.6057640.2349730.5663460.5981050.478153
20.3832300.8221740.2288550.7432580.076701
30.8752870.5766680.1769820.3418270.112582
40.2054250.8985440.7991740.0009050.377990
[2]删除不连续多行
# 删除不连续多行,删除第2和第4行df.drop(df.index[[1,3]],inplace=True)print(df)

执行结果:
01234
00.9786120.5565390.7813620.5475270.706686
20.8458220.3217160.4441760.0539150.296631
40.6177350.0408590.1292350.5251160.005357
[3]删除连续多行
# 删除连续多行df.drop(df.index[1:3],inplace=True) # 开区间,最后一个索引号不计算在内print(df)

执行结果:
01234
00.0728910.9262970.8822650.9713680.567840
30.1632120.5460690.3609900.4942740.065744
40.7529170.2421120.5266750.9187130.320725

1.2列删除
列的删除可以使用deldrop两种方式,del df[1] # 删除第2列,该种方式为原地删除,本文具体讲解drop函数删除。
[1]删除指定列
df.drop([1,3],axis=1,inplace=True) # 指定轴为列# df.drop(columns=[1,3],inplace=True) # 直接指定列

执行结果:
024
00.5928690.1233690.815126
10.1270640.0939940.332790
20.4115600.1187530.143854
30.9653170.2677400.349927
40.6886040.6996580.932645
[2]删除连续列
df.drop(df.columns[1:3],axis=1,inplace=True) #指定轴# df.drop(columns=df.columns[1:3],inplace = True) # 指定列print(df)

执行结果:
034
00.3096740.9746940.660285
10.6773280.9694400.953452
20.9541140.9535690.959771
30.3656430.4170650.951372
40.7330810.8809140.804032

2.根据自定义的行列索引操作
示例数据
df = pd.DataFrame(data=https://www.it610.com/article/np.random.rand(5,5))df.index = list('abcde')df.columns = list('一二三四五')print(df)

数据展示
一二三四五a0.1884950.5744220.5303260.8424890.474946b0.9125220.9820930.9640310.4986380.826693c0.5807890.0139570.5152290.7950520.859267d0.5406410.8656020.3052560.5525660.754791e0.3754070.2361180.1292100.7117440.067356


2.1行删除
[1]删除单行
df.drop(['b'],inplace=True)print(df)

执行结果:
一二三四五
a0.3063500.6220670.0305730.4905630.009987
c0.6724230.0716610.2745290.4000860.263024
d0.6542040.8090870.0660990.1672900.534452
e0.6289170.2326290.0701670.4699620.957898
[2]删除多行
df.drop(['b','d'],inplace=True)print(df)

执行结果:
一二三四五
a0.3915830.5098620.9246340.4665630.058414
c0.8020160.6213470.6592150.5757280.935811
e0.2233720.2861160.1305870.1135440.910859

2.2列删除
[1]删除单列
df.drop(['二'],axis=1,inplace=True)# 删除单列print(df)

执行结果:
一三四五
a0.2761470.7974040.1844720.081162
b0.6301900.3280550.4286680.168491
c0.9799580.0290320.9346260.106805
d0.7629950.0031340.1362520.317423
e0.1372110.1166070.3677420.840080
[2]删除多列
df.drop(['二','四'],axis=1,inplace=True) # 删除多列# df.drop(columns=['二','四'],inplace=True) # 删除多列print(df)

执行结果:
一三五
a0.6656470.7092430.019711
b0.9207290.9959130.490998
c0.3528160.1858020.406174
d0.1364140.5635460.762806
e0.2597100.7754220.794880
【Pandas中DataFrame数据删除详情】到此这篇关于PandasDataFrame数据删除详情的文章就介绍到这了,更多相关PandasDataFrame数据删除 原创内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    推荐阅读