简体   繁体   中英

How can i add a third if statement for pandas (python) dataset?

I have this dataset which works, but i'd like to add a 3rd ifthen statement

import pandas as pd

data = pd.read_excel('C:\\Users\\db\\Desktop\\Pandas\\sans.xls', index_col = 'Department', skiprows=1)
data.rename(columns = {'Employee Number':'Employee_Number', 'Last Name':'Last_Name', 'First Name':'First_Name', 'User Primary Organization':'Organization', 'Manager Full Name':'Manager','Manager Email':'Manager_Email','Department':'Users_Department',}, inplace=True)
new_data = data.drop(['Employee_Number', 'Last_Name', 'First_Name', 'Manager', 'Manager_Email'], axis=1)

training_type = []

for row in data.Organization:
    if row in ['CARB Training', 'Security ISO Team', 'OIS Operations and Support']:
        training_type.append('CARB Security Training')
    elif:
        training_type.append('Phshing')

new_data['training_type'] = training_type
new_data

It works as expected, but i want to add a 3rd row, based on 1 more training type

IIUC, you want to add a condition for your elif , then add more elif s or an else :

for row in data.Organization:
    if row in ['CARB Training', 'Security ISO Team', 'OIS Operations and Support']:
        training_type.append('CARB Security Training')
    elif row in ['some_other_thing', 'some_other_other_thing']:
        training_type.append('Phshing')
    else:
        training_type.append('third_thing')

I'd recommend doing this with one of the numpy methods instead, much faster.

Option 1
np.where

  • np.where with 2 conditions:

     c1 = ['CARB Training', 'Security ISO Team', 'OIS Operations and Support'] new_data['training_type'] = np.where(data.Organization.isin(c1), 'CARB Security Training', 'Phishing') 
  • np.where with 3 conditions:

     c1 = ['CARB Training', 'Security ISO Team', 'OIS Operations and Support'] c2 = [....] new_data['training_type'] = \\ np.where(data.Organization.isin(c1), 'CARB Security Training', np.where(data.Organization.isin(c2), 'SomethingElse', 'Phishing')) 

Option 2
np.select

m1 = data.Organization.isin(c1)
m2 = data.Organization.isin(c2)
m3 = m1 | m2

new_data['training_type'] = np.select([m1, m2, ~(m1 | m2)], 
              ['CARB Security Training', 'SomethingElse', 'Phishing'])

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