project.py
project.py

Reputation: 103

Putting multiple if statments in one if statment in python pandas

I need to convert this big if statement from python to python pandas. I have a problem because inside of this statement have a multiple if statements.

All values (x_value, i_value, u_value, y_val, tb, uvd) are columns in xlsx sheet. I need to create a new column with codes that are in print function. Statement is a longer than is showed here

import math

if x_value <= 1.00 and x_value >= 0:
    if y_val <= 0.25 and y_val >= 0:
        if tb < 0 and i_value == 0:
            print('aa1')
        elif tb < 0 and uvd > 0:
            print('aa2')
        elif tb < 0 and uvd < 0:
            print('aa3')
        elif tb > 0 and uvd < 0:
            print('aa4')
        elif tb > 0 and uvd > 0:
            print('aa5')
        elif tb > 0 and u_value > 0:
            print('aa6')
        else:
            print('error')
    elif y_val <= 0.50 and y_val >= 0.25:
        if tb < 0 and i_value == 0:
            print('ab1')
        elif tb < 0 and uvd > 0:
            print('ab2')
        elif tb < 0 and uvd < 0:
            print('ab3')
        elif tb > 0 and uvd < 0:
            print('ab4')
        elif tb > 0 and uvd > 0:
            print('ab5')
        elif tb > 0 and u_value > 0:
            print('ab6')
        else:
            print('error')
    elif y_val <= 0.75 and y_val >= 0.50:
        if tb < 0 and i_value == 0:
            print('ac1')
        elif tb < 0 and uvd > 0:
            print('ac2')
        elif tb < 0 and uvd < 0:
            print('ac3')
        elif tb > 0 and uvd < 0:
            print('ac4')
        elif tb > 0 and uvd > 0:
            print('ac5')
        elif tb > 0 and u_value > 0:
            print('ac6')
        else:
            print('error')
    elif y_val <= 1.00 and y_val >= 0.75:
        if tb < 0 and i_value == 0:
            print('ad1')
        elif tb < 0 and uvd > 0:
            print('ad2')
        elif tb < 0 and uvd < 0:
            print('ad3')
        elif tb > 0 and uvd < 0:
            print('ad4')
        elif tb > 0 and uvd > 0:
            print('ad5')
        elif tb > 0 and u_value > 0:
            print('ad6')
        else:
            print('error')
    else:
        print('Error!')

elif x_value <= 3.00 and x_value >= 1:
    if y_val <= 0.25 and y_val >= 0:
        if tb < 0 and i_value == 0:
            print('ba1')
        elif tb < 0 and uvd > 0:
            print('ba2')
        elif tb < 0 and uvd < 0:
            print('ba3')
        elif tb > 0 and uvd < 0:
            print('ba4')
        elif tb > 0 and uvd > 0:
            print('ba5')
        elif tb > 0 and u_value > 0:
            print('ba6')
        else:
            print('error')
    elif y_val <= 0.50 and y_val >= 0.25:
        if tb < 0 and i_value == 0:
            print('bb1')
        elif tb < 0 and uvd > 0:
            print('bb2')
        elif tb < 0 and uvd < 0:
            print('bb3')
        elif tb > 0 and uvd < 0:
            print('bb4')
        elif tb > 0 and uvd > 0:
            print('bb5')
        elif tb > 0 and u_value > 0:
            print('bb6')
        else:
            print('error')
    elif y_val <= 0.75 and y_val >= 0.50:
        if tb < 0 and i_value == 0:
            print('bc1')
        elif tb < 0 and uvd > 0:
            print('bc2')
        elif tb < 0 and uvd < 0:
            print('bc3')
        elif tb > 0 and uvd < 0:
            print('bc4')
        elif tb > 0 and uvd > 0:
            print('bc5')
        elif tb > 0 and u_value > 0:
            print('bc6')
        else:
            print('error')
    elif y_val <= 1.00 and y_val >= 0.75:
        if tb < 0 and i_value == 0:
            print('bd1')
        elif tb < 0 and uvd > 0:
            print('bd2')
        elif tb < 0 and uvd < 0:
            print('bd3')
        elif tb > 0 and uvd < 0:
            print('bd4')
        elif tb > 0 and uvd > 0:
            print('bd5')
        elif tb > 0 and u_value > 0:
            print('bd6')
        else:
            print('error')
    else:
        print('Error!')
else:
    print('ERROR')

Any suggestions?

Upvotes: 2

Views: 95

Answers (1)

yuanzz
yuanzz

Reputation: 1968

  1. I think you may want get some function in pandas.
    1. generate a toy dataset
import numpy as np
import pandas as pd


data = pd.DataFrame(np.random.random(size=(10, 6)))
data.columns = ['x_value', 'i_value', 'u_value', 'y_val', 'tb', 'uvd']
data

enter image description here

    1. generate a small function
def cal_value(x):
    if x['x_value'] < 0.5 and x['y_val'] > 0.5:
        return 'type1'
    elif x['i_value'] < 0.5 and x['tb'] > 0.5:
        return 'type2'
    else:
        return 'type3'
    1. use apply function in DataFrame
data['type'] = data.apply(lambda x: cal_value(x), axis=1)
data

enter image description here

if want get more detail about apply , click pandas documents link: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.apply.html

Upvotes: 2

Related Questions