简体   繁体   中英

pandas operations inside a for-loop

Here is a sample of my data

threats =
                                  binomial_name
    continent        threat_type    
    Africa  Agriculture & Aquaculture   143
              Biological Resource Use   102
                    Climate Change       3
               Commercial Development   36
           Energy Production & Mining   30
    ... ... ...
    South America   Human Intrusions    1
                     Invasive Species   3
        Natural System Modifications    1
              Transportation Corridor   2
                             Unknown    38

I want to use a for loop and obtain and append together the top 5 values of each continent into a data frame. Here is my code -

continents = threats.continent.unique()

for i in continents:
    continen = (threats
           .query('continent == i')
           .groupby(['continent','threat_type'])
           .sort_values(by=('binomial_name'), ascending=False).
           .head())

    top5 = appended_data.append(continen)    

I am however getting the error - KeyError: 'i'

Where am I going wrong?

So, the canonical way to do this:

df.groupby('continent', as_index=False).apply(
    lambda grp: grp.nlargest(5, 'binomial_value'))

If you want to do this in a loop, replace this part:

for i in continents: 
    continen = threats[threats['continent'] == i].nlargest(2, 'binomial_name') 
    appended_data.append(continen) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM