Python 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]}
df = pd.DataFrame(data=my_dict)
df.drop(columns='ID')
print(df)
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=df.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
Remove all rows and keep the columns.
df.drop(df.index,inplace=True)
labels
We can add the option lables to delete index or column lables.
df.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.
df.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.
df.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
df.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.
df.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 row which id is given.
df.drop(df[df['ID'] == 2].index, inplace = True) # remove matching value
#df.drop(df[df['NAME'] == 'Ravi'].index, inplace = True)
We can keep some rows based on condition and remove rest of the rows, here we are not using drop() and using loc
df=df.loc[df["MATH"] >= 45 ] # Keep all rows based on condition
df=df.loc[df["ID"] == 3 ] # Keep only row based on condition
Delete rows with multiple matching conditions. Here & ( and ) is used to match both the conditions. Here one record is deleted.
my_delete=df[(df['NAME']=='Ravi') & (df['ID']==1)].index
df.drop(my_delete,inplace=True)
Condition can be changed by using OR matching , here two records will be deleted.
my_delete=df[(df['NAME']=='Ravi') | (df['ID']==2)].index
Delete records with more than 256 length in column Linking page .
my_delete=df[df['Linking page'].str.len() >256].index
df.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
df.drop([1,3,7],inplace=True)
Delete last two rows
df.drop(df.tail(2).index,inplace=True)
OR ( select all without last two )
df = df.iloc[:-2]
« Pandas
Plotting graphs
Filtering of Data
← Subscribe to our YouTube Channel here