How to delete rows from pandas dataframe?

To delete a row or n rows from a pandas dataframe you can use following command

DataFrame.drop(labels=None, axis=0, index=None, columns=None, level=None, inplace=False, errors='raise')

Let's assume you have a dataframe with 100 employees data with employee id's ranging from 1 to 100. If you want to drop all employees from dataframe with employee id between 5 and 10. Then you can use following command to do this.

df.drop(df.loc[((df.emp_id >= 5) & (df.emp_id <= 10))].index, inplace=True)

If you want to drop all employees with employee id greater than 90. Then you can modify above as following.

df.drop(df.loc[df.emp_id > 90].index, inplace=True)

 

For more such tips stay tuned.



Was this article helpful?

Top Related Jobs

Top Freelance jobs are waiting for you, View & apply to earn more.



People also searched for

You are freelancing Ninja?

Try Auto proposal today, The new virtual assistant for your freelancing world.