简体   繁体   中英

Creating functions with unspecified number of returns

I have a simple code that takes three dataframes (Pandas library) and assigns values of the 'PROLIFIC_PID' column as the index of that dataframe

def pid_set(a, b, c): #accepts three dataframes at a time and returns dataframes with same name with PID as index
    a = a.set_index('PROLIFIC_PID')
    b = b.set_index('PROLIFIC_PID')
    c = c.set_index('PROLIFIC_PID')
    return a, b, c

I wanted to change the above code so that it takes X number of dataframes, sets PID as index, and returns X number of new dataframes. I tried multiple variations of the code above with no luck-- tried putting indexed dataframes in a list and return just the list, I have tried "pass" as the return etc-- but they do not function as I had hoped.

You can just let X be a list of dataframes, like

X = [a, b, c, ...]

def pid_set(X):
    for i in range(len(X)):
        X[i] = X[i].set_index("PROLIFIC_PID")
    return X

And by the way, you don't have to return those dataframes, they will be updated even outside of the pid_set function.

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