DPs
DPs

Reputation: 227

if and elif condition based on values of a dataframe

I'm trying to create a new columns based on the columns that are present in the dataframe. This is the sample data

     ORG   CHAIN_NBR  SEQ_NBR     INT_STATUS  BLOCK_CODE_1  BLOCK_CODE_2
0   523         1        0          A             C             A
1   523         2        1          I             A             D
2   521         3        1          A             H             F
3   513         4        1          D             H             Q
4   513         5        1          8             I             M

This is the code that I'm Executing:

df=pd.read_csv("rtl_one", sep="\x01")


    def risk():
        if df['INT_STATUS'].isin(['B','C','F','H','P','R','T','X','Z','8','9']):
            df['rcut'] = '01'
        elif df['BLOCK_CODE_1'].isin(['A','B','C','D','E','F','G','I','J','K','L','M', 'N','O','P','R','U','W','Y','Z']):
            df['rcut'] = '02' 
        elif df["BLOCK_CODE_2"].isin(['A','B','C','D','E','F','G','I','J','K','L','M', 'N','O','P','R','U','W','Y','Z']):
            df['rcut'] == '03'

        else:
            df['rcut'] = '00'

    risk()

Output data should look like this:

     ORG   CHAIN_NBR  SEQ_NBR   INT_STATUS  BLOCK_CODE_1  BLOCK_CODE_2 rcut 
0   523         1        0          A             C             A      02
1   523         2        1          I             A             D      02
2   521         3        1          A             H             F      03
3   513         4        1          D             H             Q      00
4   513         5        1          8             I             M      01

Upvotes: 0

Views: 686

Answers (2)

Dev Anand
Dev Anand

Reputation: 64

use .index to add a new column, df['rcut'] = df.index and then use df.insert( index,'rcut',value) in your if elif condition.

Upvotes: 0

user2969402
user2969402

Reputation: 1251

use iterrows and store the results in a list that you can then append to the dataframe as a column:

rcut = []

for i, row in df.iterrows():
    if row['INT_STATUS'] in ['B','C','F','H','P','R','T','X','Z','8','9']:
        rcut.append('01')
    elif row['BLOCK_CODE_1'] in ['A','B','C','D','E','F','G','I','J','K','L','M', 'N','O','P','R','U','W','Y','Z']:
        rcut.append('02')
    elif row['BLOCK_CODE_1'] in ['A','B','C','D','E','F','G','I','J','K','L','M', 'N','O','P','R','U','W','Y','Z']:
        rcut.append('03')
    else:
        rcut.append('00')
df['rcut'] = rcut

(Note: your 2nd and 3d conditions are the same, I've reused your code here so you would have to change that)

Upvotes: 2

Related Questions