pandas dataframe drop函数介绍

使用drop函数删除dataframe的某列或某行数据:

drop(labels, axis=0, level=None, inplace=False, errors='raise')
         --  axis为0时表示删除行,axis为1时表示删除列

常用参数如下: 

pandas dataframe drop函数介绍_第1张图片

import pandas as pd
import numpy as np
 
data = {'Country':['China','US','Japan','EU','UK/Australia', 'UK/Netherland'],
'Number':[100, 150, 120, 90, 30, 2],
'Value': [1, 2, 3, 4, 5, 6],
'label': list('abcdef')}
 
df = pd.DataFrame(data)
print("df原数据:\n", df, '\n')
out:
df原数据:
          Country  Number  Value label
0          China     100      1     a
1             US     150      2     b
2          Japan     120      3     c
3             EU      90      4     d
4   UK/Australia      30      5     e
5  UK/Netherland       2      6     f

删除单列:

print(df.drop('Country', axis = 1))
 
out:
   Number  Value label
0     100      1     a
1     150      2     b
2     120      3     c
3      90      4     d
4      30      5     e
5       2      6     f

删除多列:

print(df.drop(['Country','Number'], axis = 1))
 
out:
   Value label
0      1     a
1      2     b
2      3     c
3      4     d
4      5     e
5      6     f

删除单行:

print(df.drop(labels = 1, axis = 0))
 
out:
         Country  Number  Value label
0          China     100      1     a
2          Japan     120      3     c
3             EU      90      4     d
4   UK/Australia      30      5     e
5  UK/Netherland       2      6     f

删除多行:

print(df.drop(labels = [1,2], axis = 0))
 
out:
         Country  Number  Value label
0          China     100      1     a
3             EU      90      4     d
4   UK/Australia      30      5     e
5  UK/Netherland       2      6     f

使用range函数删除连续多行:

print(df.drop(labels = range(1,3), axis = 0))
 
out:
         Country  Number  Value label
0          China     100      1     a
3             EU      90      4     d
4   UK/Australia      30      5     e
5  UK/Netherland       2      6     f

到此这篇关于pandas dataframe drop函数介绍的文章就介绍到这了,更多相关pandas dataframe drop 内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的:(pandas dataframe drop函数介绍)