Using the drop() function of python pandas you can drop or remove :
- Specific row or column
- multiple rows or columns
from the dataframe
Syntax:
DataFrame.drop(self, labels=None, axis=0, index=None, columns=None, level=None, inplace=False, errors='raise')
drop()
drop(labels=[1,5])
drop([1,5])
how to drop column
df.drop(['name','available'],axis=1)
you can do the same work with parmeter index and columns
remember : you Cannot specify both 'labels' and 'index'/'columns'
df.drop(['name','available'],axis=1,index=[1,5]) gives ValueError
df.drop(index=[1,5])
df.drop(columns=['name','available'])
you can remove rows and collumns together
using
df.drop(index=[1,5],columns=['name','available'])
df.drop(index=[1,9],columns=['name','available']) gives key error becuase index 9 is not present
if you want to ignore error and want to drop or delete only existing labels are dropped.
df.drop(index=[1,9],columns=['name','available'],errors='ignore')
level
df=pd.read_csv('/home/neeraj/nn/test2.csv',index_col=['name','count'])
df
df.drop(index=[‘ff’],level=0)
df.drop(index=[23],level=1)
Visit our website www.metazonetrainings.com for best experience.
You can also join us on Facebook: / metazoneinfotech
24 апр 2020