简体   繁体   中英

Insert several new column with the values based on another columns in a Dataframe in pandas

I have a dataframe of the variable shape. There are two fixed columns at the start plus flexible number of columns that will be different for every case. Starting dataframe:

A        B         FlexColumnA    FlexColumnB
Apples   Pears     0/1;23;45;67   1/1;23;45;67
Apples   Apples    0/0;24;26;27   0/1;27;28;29

I need to insert a new column before every "FlexColumn" with the following rules:

Name of a new column: "FlexColumn"+my_ending; my_ending is constant;

Values of a new column: "If a row of the flexible column contains '1/1', then 'norm'; if a row contains o/1, then insert 'half'; otherwise -'not_known' "

A         B        FlexColumnA_myEnding  FlexColumnA    FlexColumnB_myEnding FlexColumnB
Apples   Pears     half                  0/1;23;45;67   norm                 1/1;23;45;67
Apples   Apples    not_known             0/0;24;26;27   half                 0/1;27;28;29

How about something like this:

def get_outcome(s):
    if '1/1' in s:
        return 'norm'
    elif '0/1' in s:
        return 'half'
    else:
        return 'not_known'

flexcols = [c for c in df.columns if 'FlexColumn' in c]

new_cols = []

for col in flexcols:
    new_cols.append(df[col].apply(get_outcome).rename(col+'_myEnding'))
    new_cols.append(df[col])

pd.concat([df['A'],df['B']]+new_cols,axis=1)

在此处输入图像描述

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