简体   繁体   中英

Multiple Conditional Statements in Pandas DataFrame

I'm trying to replicate multiple nested IF statements from an Excel file and apply it to a Pandas DataFrame without looping through each individual value if possible. I have a 10x10000 Dataframe of random numbers and basically want to create a new DataFrame by replicating this Excel code for only Row 1 of 10:

=IF(D16<0.25,1,IF(D16<0.5,2,IF(D16<0.75,3,4)))

This output is in D27. In Rows 2-10:

=IF(D27=1,IF(D17<$E$3,1,3),IF(D27=2,IF(D17<$E$3,1,3),IF(D27=3,IF(D17<$E$4,2,4),IF(D27=4,IF(D17<$E$4,2,4)))))

E3,E4 are hard coded numbers. So there is multiple conditional and references to previous value. Here is the code to create the 10x10000 random matrix:

import pandas as pd
import numpy as np
randomvars = pd.DataFrame(np.random.randint(0,100,size=(10,10000)))/100

Thanks!

For the first IF statement I used a simple statement:

Econstate2=pd.DataFrame(np.where(randomvars<0.25,1,np.where(randomvars<.5,2,np.where(randomvars<.75,3,4))))

However I couldn't get around using a complex nested IF for the second IF above:

for x in range(0,len(randomvars.columns)):
    for i in range(1,len(randomvars)):
        if Econstate2.loc[i-1,x] == 1 or Econstate2.loc[i-1,x] == 2:
            if randomvars.loc[i,x] < .88:
                Econstate2.loc[i,x] = 1
            else:
                Econstate2.loc[i,x] = 3
        elif Econstate2.loc[i-1,x] == 3 or Econstate2.loc[i-1,x] == 4:
            if randomvars.loc[i,x] < .80:
                Econstate2.loc[i,x] = 3
            else: 
                Econstate2.loc[i,x] = 4

Hope this helps someone and thanks for those that tried!

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