pandas中drop()函数用法

函数定义:DataFrame.drop(labels=None,axis=0, index=None, columns=None, inplace=False)
删除单个行

import pandas as pd
import numpy as np
from pandas import Series, DataFrame
data = DataFrame(np.arange(16).reshape((4, 4)),index = ['Ohio', 'Colorado', 'Utah', 'New York'], columns = ['one', 'two', 'three', 'four'])
print(data)
print(data.drop('Ohio'))

axis=0,指删除index,因此删除columns时要指定axis=1
pandas中drop()函数用法_第1张图片删除多个行

import pandas as pd
import numpy as np
from pandas import Series, DataFrame
data = DataFrame(np.arange(16).reshape((4, 4)),index = ['Ohio', 'Colorado', 'Utah', 'New York'], columns = ['one', 'two', 'three', 'four'])
print(data)
print(data.drop(['Ohio','Utah']))

axis=0,指删除index,因此删除columns时要指定axis=1
pandas中drop()函数用法_第2张图片
在没有取行名或列名的情况下,可以按一下方式删除行或列

import pandas as pd
import numpy as np
from pandas import Series, DataFrame
data = DataFrame(np.arange(16).reshape((4, 4)), columns = ['one', 'two', 'three', 'four'])
print(data)
print(data.drop([0,1]))

pandas中drop()函数用法_第3张图片

你可能感兴趣的:(pandas中drop()函数用法)