Pandas DataFrame drop()

Pandas

We can drop rows or columns by using drop().

This returns DataFrame without the removed index or column labels.
import pandas as pd 
my_dict={'NAME':['Ravi','Raju','Alex','Ron','King','Jack'],
         'ID':[1,2,3,4,5,6],
         'MATH':[80,40,70,70,70,30],
         'ENGLISH':[80,70,40,50,60,30]}
my_data = pd.DataFrame(data=my_dict)
my_data.drop(columns='ID')
print(my_data)
Output
   NAME  ID  MATH  ENGLISH
0  Ravi   1    80       80
1  Raju   2    40       70
2  Alex   3    70       40
3   Ron   4    70       50
4  King   5    70       60
5  Jack   6    30       30
We can see ID column is not deleted. Here the original DataFrame is retained. Now we will change the last lines like this.
df=my_data.drop(columns='ID')
print(df)
Now our output will change by removing ID column.
   NAME  MATH  ENGLISH
0  Ravi    80       80
1  Raju    40       70
2  Alex    70       40
3   Ron    70       50
4  King    70       60
5  Jack    30       30

labels

We can add the option lables to delete index or column lables.
my_data.drop(labels=[2,4])
Output
NAME	ID	MATH	ENGLISH
0	Ravi	1	80	80
1	Raju	2	40	70
3	Ron	4	70	50
5	Jack	6	30	30
Now Index 2 and 4 are removed.

labels

We will remove columns by using labels option.
my_data.drop(labels=['ID','MATH'],axis=1)
Two columns ID and MATH are removed.
	NAME	ENGLISH
0	Ravi	80
1	Raju	70
2	Alex	40
3	Ron	50
4	King	60
5	Jack	30

columns

We can input single or a list of columns to remove.
my_data.drop(columns=['ID','MATH'])
Output
	NAME	ENGLISH
0	Ravi	80
1	Raju	70
2	Alex	40
3	Ron	50
4	King	60
5	Jack	30

index

We can use single label or list
my_data.drop(index=2,columns='MATH')
Output
	NAME	ID	ENGLISH
0	Ravi	1	80
1	Raju	2	70
3	Ron	4	50
4	King	5	60
5	Jack	6	30
index =2 or row 3 is removed.
my_data.drop(index=2,axis=0)
Output

NAME	ID	MATH	ENGLISH
0	Ravi	1	80	80
1	Raju	2	40	70
3	Ron	4	70	50
4	King	5	70	60
5	Jack	6	30	30

errors

Default value is 'raise'. We can set it to errors='ignore'

Deleting rows

Delete records with more than 256 length.
my_delete=my_data[my_data['Linking page'].str.len() >256].index
my_data.drop(my_delete,inplace=True)
Pandas Plotting graphs Filtering of Data


plus2net.com



Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer