简体   繁体   中英

How to loop a command in python with a list as variable input?

This is my first post to the coding community, so I hope I get the right level of detail in my request for help!

Background info:
I want to repeat (loop) command in a df using a variable that contains a list of options. While the series ' amenity_options ' contains a simple list of specific items (let's say only four amenities as the example below) the df is a large data frame with many other items. My goal is the run the operation below for each item in the ' amenity_option ' until the end of the list.

amenity_options = ['cafe','bar','cinema','casino'] # this is a series type with multiple options

df = df[df['amenity'] == amenity_options] # this is my attempt to select the the first value in the series (e.g. cafe) out of dataframe that contains such a column name.

df.to_excel('{}_amenity.xlsx, format('amenity') # wish to save the result (e.g. cafe_amenity) as a separate file.

Desired result:
I wish to loop step one and two for each and every item available in the list (eg cafe, bar, cinema...). So that I will have separate excel files in the end. Any thoughts?

Seems like you just need a simple for loop:

for amenity in amenity_options:
    df[df['amenity'] == amenity].to_excel(f"{amenity}_amenity.xlsx")

What @Rakesh suggested is correct, you probably just need one more step.

df = df[df['amenity'].isin(amenity_options)]

for key, g in df.groupby('amenity'):
    g.to_excel('{}_amenity.xlsx'.format(key))

After you call groupby() of your df, you will get 4 groups so that you can directly loop on them.

The key is the group key, which are cafe , bar and etc. and the g is the sub-dataframe that specifically filtered by that key.

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