[Tutor] Delete unwanted rows
Mark Lawrence
breamoreboy at yahoo.co.uk
Wed Apr 9 15:38:36 CEST 2014
On 09/04/2014 10:23, Alexis Prime wrote:
> Hello,
>
> My question is whether I should write a loop or a function to delete rows.
>
> I'm using pandas. But you may be able to help me as my question is about
> the reasoning behind programming.
>
> I have a pandas dataframe that looks like this, covering all countries
> in the world, for over 200 rows and many columns:
>
> Canada 20
> China 112
> Germany 10
> Japan 12
> Martinique 140
> Mexico 180
> Saint Kitts 90
> Saint Martins 133
> Saint Helena 166
> USA 18
>
> # So I write a list of small countries that I wish to exclude from my
> analysis. What I want to do is to delete the rows from my dataframe.
>
> toexclude = ['Martinique', 'Saint Kitts', 'Saint Martins', 'Saint
> Helena']
>
> After this, should I write a loop to loop through the dataframe, find
> the countries that I want to delete, and then delete the rows?
>
> Or should I write a function, which deletes those rows, and then returns
> me a new and trimmed dataframe?
>
> Thank you for helping me figure this out.
>
> Alexis
>
If you are going to do this repeatedly you'd be better off writing a
function. I don't know enough about pandas to say whether the rows
should be deleted within a loop in the function or a new data frame is
returned. But before you write any code, have to checked to see if
there is a function or method in pandas that does this for you?
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
Mark Lawrence
---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com
More information about the Tutor
mailing list