Search code examples
python-3.xpandaslistdataframepandas.excelwriter

Create new column and calculate values to the column in python row wise


I need to create a new column as Billing and Non-Billing based on the Billable column. If the Billable is 'Yes' then i should create a new column as Billing and if its 'No' then need to create a new column as 'Non-Billable' and need to calculate it. Calculation should be in row axis.

  • Calculation for Billing in row: Billing = df[Billing] * sum/168 * 100

  • Calculation for Non-Billing in row: Non-Billing = df[Non-Billing] * sum/ 168 * 100

Data

Employee Name  |    Java  |  Python| .Net  |  React |  Billable|                                  
----------------------------------------------------------------                                                 
|Priya         |    10    |        |   5   |        |  Yes     |                                                  
|Krithi        |          |   10   |   20  |        |   No     |                                                   
|Surthi        |          |   5    |       |        |  yes     |                                                
|Meena         |          |   20   |       |    10  |   No     |                                                 
|Manju         |    20    |  10    |  10   |        |  Yes     |       
                                     

Output

I have tried using insert statement but i cannot keep on inserting it. I tried append also but its not working.

Bill_amt = []
Non_Bill_amt = []

for i in df['Billable']:
    if i == "Yes" or i == None:
        Bill_amt = (df[Bill_amt].sum(axis=1)/168 * 100).round(2)
        df.insert (len( df.columns ), column='Billable Amount', value=Bill_amt )#inserting the column and it name
        #CANNOT INSERT ROW AFTER IT AND CANNOT APPEND IT TOO
    else:
        Non_Bill_amt = (DF[Non_Bill_amt].sum ( axis=1 ) / 168 * 100).round ( 2 )
        df.insert ( len ( df.columns ), column='Non Billable Amount', value=Non_Bill_amt ) #inserting the column and its name
        #CANNOT INSERT ROW AFTER IT.

Solution

  • Use .sum(axis=1) and then np.where() to put the values in respective columns. For example:

    x = df.loc[:, "Java":"React"].sum(axis=1) / 168 * 100
    
    df["Bill"] = np.where(df["Billable"].str.lower() == "yes", x, "")
    df["Non_Bill"] = np.where(df["Billable"].str.lower() == "no", x, "")
    
    print(df)
    

    Prints:

      Employee_Name  Java  Python  .Net  React Billable                Bill            Non_Bill
    0         Priya  10.0     NaN   5.0    NaN      Yes   8.928571428571429                    
    1        Krithi   NaN    10.0  20.0    NaN       No                      17.857142857142858
    2        Surthi   NaN     5.0   NaN    NaN      yes   2.976190476190476                    
    3         Meena   NaN    20.0   NaN   10.0       No                      17.857142857142858
    4         Manju  20.0    10.0  10.0    NaN      Yes  23.809523809523807