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?



You are freelancing Ninja?

Try Toogit Instant Connect today, The new virtual assistant for your freelancing world.


What is Python Script?

Python is an interpreted, object-oriented, high-level programming language with dynamic semantics.

Python's simple, easy to learn syntax emphasizes readability and therefore reduces the cost of program maintenance.

Python supports modules and packages, which encourages program modularity and code reuse.

What is a Python Script Freelancer?

Python is an interpreted, object-oriented and extensible programming language. Python can run on many different operating systems.

A freelancer well versed in Python can handle your workload quite easily. To hire freelance programming help for Python post a job today!

What is a Freelancer?

A freelancer or freelance worker, is a term commonly used for a person who is self-employed and is not necessarily committed to a particular employer long-term.

Why hire a Freelancer instead of full time employee?

If there is a long lead time for them to get up and running, using that investment on a full-time employee might be a better option. And if the position requires oversight, hire an employee.

A freelancer might choose to perform the work outside of normal business hours, when you're not able to monitor their progress.