vendredi 19 juin 2020

How to deal with a Dataframe with many columns and if statement

Here is my problem

You will find below a sample of my DataFrame:

df = pd.DataFrame({'Date':['01/03/2000','01/04/2000','01/05/2000','01/06/2000','01/07/2000','01/08/2000'],
                      'Paul_Score':[3,10,22,32,20,40],
                       'John_Score':[8,42,10,57,3,70]

                      })

df['Date']= pd.to_datetime(df['Date'])

df = df.set_index('Date')

And I started to work on a loop with an If statement like this:

def test(selection,symbol):
    df_end = (selection*0)
    rolling_mean = selection.rolling(2).mean().fillna(0)
    calendar = pd.Series(df_end.index)

    for date in calendar:
        module=1/selection.loc[date,symbol]
        if  selection.loc[date,symbol] > rolling_mean.loc[date,symbol]:
            df_end.loc[date,symbol] = module

        else:
            df_end.loc[date,symbol]=0


    return df_end

Then :

test(df,'John_Score')

However, my problem is that I don't know how to deal with many columns at the same time, my goal is to try this function on the whole dataframe (for all columns). This sample has only 2 columns but in reality I have 30 columns and I don't know how to do it.

If you have any idea, you are welcome

Aucun commentaire:

Enregistrer un commentaire