« Pandas
We can drop rows or columns by using drop().
DataFrame.drop(labels=None, axis=0, index=None, columns=None,
level=None, inplace=False, errors='raise')
This returns DataFrame without the removed index or column labels.
Here drop() does not change the original DataFrame, so after dropping the rows or column if we display the same or original dataframe then we will not find any change in rows or columns.
Use inplace=True to update the DataFrame.
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,inplace=True)
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)
Delete with and ( & ) combination ( two conditions to match ) . Lenth of data in Page column is less than 12 and more than 3 to be deleted.
my_delete=df[(df['Page'].str.len() <12) & (df['Page'].str.len() >3)].index
Delete specific rows
Delete 1 ,3 and 7 rows
my_data.drop([1,3,7],inplace=True)
Delete last two rows
my_data.drop(df.tail(2).index,inplace=True)
OR ( select all without last two )
my_data = my_data.iloc[:-2]
« Pandas
Plotting graphs
Filtering of Data
← Subscribe to our YouTube Channel here