Home > Back-end >  How to I create a new column in pandas using if/elif conditions on multiple lists
How to I create a new column in pandas using if/elif conditions on multiple lists

Time:11-16

I am trying to create a new column in my dataset to classify soil texture into 3 textural classes. I have a column ('texture') containing 6 different soil texture.

I created 3 lists as shown below:

Defining soil textural column (ie fine, coarse and medium)

# Defining soil textural column (ie fine, coarse and medium)
coarse_tex = ['Sand', 'Loamy Sand' 'Sandy Loam']
medium_tex = ['Loam', 'Silt Loam', 'Silt', 'Sandy Clay Loam']
fine_tex = ['Clay Loam', 'Silty Clay Loam', 'Sandy Clay', 'Silty Clay', 'Clay']

I then defined a function to classify soil texture into coarse, medium and fine using as shown below:

# Define a function to classify soil texture into 3 textural classes
def texture_classifier(texture):
    if sha_df['texture'].isin(coarse_tex):
        tex_class = 'coarse'
    elif sha_df['texture'].isin(medium_tex):
        tex_class = 'medium'
    elif sha_df['texture'].isin(fine_tex):
        tex_class = 'fine'
    return tex_class

Finally, I tried apply the function to create a new column as shown below:

sha_df['textural_class'] = sha_df.apply(texture_classifier)

After all that, I keep getting the ValueError:

enter image description here

Any help please

I tried the code in the image below but got errors enter image description here

I want help on getting an output as shown below:

textural_class
coarse
medium
fine

CodePudding user response:

try this:

import numpy as np
cond1 = sha_df['texture'].isin(coarse_tex)
cond2 = sha_df['texture'].isin(medium_tex)
cond3 = sha_df['texture'].isin(fine_tex)

sha_df['textural_class'] = np.select(
    [cond1, cond2, cond3],
    ['coarse_tex', 'medium_tex', 'fine_tex'],
    None
)
  • Related