How to retain the application of multiple np.where() conditions

I have the following df, containing allocations for Stratification groups in a randomized controlled trial.

import numpy as np
import pandas as pd

df = pd.DataFrame([[1, "ABABBBAAAB"], [2, "BBABBBAAAA"], [3, "ABBAABABAB"]], columns=['StratID', 'Rand'])

df

    StratID Rand
0   1   ABABBBAAAB
1   2   BBABBBAAAA
2   3   ABBAABABAB

I want to use np.where to trim the length of the Stratification groups based on the StratID, so for example StratID 1 and 3 should be trimmed to only retain the first 6 allocations

df["trimmed_col"] = np.where(df["StratID"].isin(
    {1, 3}), df.Rand.str[:6], "") 

df
    StratID Rand    trimmed_col
0   1   ABABBBAAAB  ABABBB
1   2   BBABBBAAAA  
2   3   ABBAABABAB  ABBAAB 

But when I go to do this for the last remaining StratID 3 I overwrite what I've done above and get the following.

df["trimmed_col"] = np.where(df["StratID"].isin(
    {2}), df.Rand.str[:4], "") 

df
    StratID Rand    trimmed_col
0   1   ABABBBAAAB  
1   2   BBABBBAAAA  BBAB
2   3   ABBAABABAB  

How can I apply both changes to the dataframe at once so I get the following output

    StratID Rand    trimmed_col
0   1   ABABBBAAAB  ABABBB
1   2   BBABBBAAAA  BBAB
2   3   ABBAABABAB  ABBAAB


from Recent Questions - Stack Overflow https://ift.tt/3sUughg
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Hibernate Search - Elasticsearch with JSON manipulation

Spring Elasticsearch Operations