Home > other >  How can I append values to two columns based on condition of a third column in pandas?
How can I append values to two columns based on condition of a third column in pandas?

Time:03-17

I want to add x=0 and y = 1.30 or 1.60 depending on value of the column 'Treatment'. Currently I only append one of the two values of y (in this example 1.60), but this is not entirely correct as the value should be 1.30 for treatment 'White_FR'.

This is the dataframe

df_tot

       Treatment        y        x     Individual 
0       White       21.982733   800   Data20210608
1       White       21.973003   800   Data20210508  
2       White       21.968242   800   Data20210408  
15      White_FR    22.139293   800   Data20210608  
16      White_FR    22.159840   800   Data20210508  
17      White_FR    22.162254   800   Data20210408  

I would like to get something like:

df_tot_new

       Treatment        y        x     Individual 
        White       21.982733   800  Data20210608
        White       1.60        0    Data2021608  #new
        White       21.973003   800  Data20210508   
        White       1.60        0    Data2021608 #new
        White       21.968242   800  Data20210408   
        White       1.60        0    Data2021608 #new
        White_FR    22.139293   800  Data20210608
        White_FR    1.30        0    Data2021608 #new
        White_FR    22.159840   800  Data20210508   
        White_FR    1.30        0    Data2021608 #new
        White_FR    22.162254   800  Data20210408    
        White_FR    1.30        0    Data2021608 #new

This is the code:

 df_tot['y'] = df_tot.append([{'y':1.60}, {'x':0}], ignore_index=True)
 

CodePudding user response:

In this case, it would be easier to duplicate the dataframe, modify the copy, and then concatenate them together and sort by the index:

tmp = df.copy()
tmp['y'] = tmp['Treatment'].map({'White': 1.60, 'White_FR': 1.30})
tmp['x'] = 0

df = pd.concat([df, tmp]).sort_index().reset_index(drop=True)

Output:

>>> df
   Treatment          y    x    Individual
0      White  21.982733  800  Data20210608
1      White   1.600000    0  Data20210608
2      White  21.973003  800  Data20210508
3      White   1.600000    0  Data20210508
4      White  21.968242  800  Data20210408
5      White   1.600000    0  Data20210408
6   White_FR  22.139293  800  Data20210608
7   White_FR   1.300000    0  Data20210608
8   White_FR  22.159840  800  Data20210508
9   White_FR   1.300000    0  Data20210508
10  White_FR  22.162254  800  Data20210408
11  White_FR   1.300000    0  Data20210408
  • Related