How to get values from a Column in pandas if another column in same dataframe matches a condition?

Question:

     Tax_Amount Rate    SGST    CGST    IGST    TDS Tax_Term

0   5697.0  9.0 NaN NaN NaN NaN CGST
1   954.0   9.0 NaN NaN NaN NaN TDS
2   1305.0  9.0 NaN NaN NaN NaN CGST
3   2724.0  9.0 NaN NaN NaN NaN SGST
4   18000.0 9.0 NaN NaN NaN NaN IGST

i Have a Dataframe as above, I want to have the Tax_Amount in all respective column if it matches the Colum Tax_Term.
Expected Output:-

    Tax_Amount  Rate    SGST    CGST    IGST    TDS Tax_Term
0   5697.0  9.0 NaN 5697.0  NaN NaN CGST
1   954.0   9.0 NaN 954.0   NaN 954.0   TDS
2   1305.0  9.0 NaN 1305.0  NaN NaN CGST
3   2724.0  9.0 2724.0  NaN NaN NaN SGST
4   18000.0 9.0 NaN NaN 18000.0 NaN IGST

I tried doing the same with below code however i didn’t get the desired result.

final_df['SGST'] = final_df.query('Tax_Term == SGST')['Tax_Amount']
final_df['CGST'] = final_df.query('Tax_Term == CGST')['Tax_Amount']
final_df['IGST'] = final_df.query('Tax_Term == IGST')['Tax_Amount']
final_df['TDS'] = final_df.query('Tax_Term == TDS')['Tax_Amount']

any help will be appretiated.
Thanks.

Asked By: Paras Chaudhary

||

Answers:

You can use the apply method to achieve this. The apply method allows you to apply a function to each row or column of a DataFrame.

Here is an example of how you can use the apply method to accomplish your task:

def fill_tax_amount(row):
    if row['Tax_Term'] == 'SGST':
        row['SGST'] = row['Tax_Amount']
    elif row['Tax_Term'] == 'CGST':
        row['CGST'] = row['Tax_Amount']
    elif row['Tax_Term'] == 'IGST':
        row['IGST'] = row['Tax_Amount']
    elif row['Tax_Term'] == 'TDS':
        row['TDS'] = row['Tax_Amount']
    return row

final_df = final_df.apply(fill_tax_amount, axis=1)

This will apply the fill_tax_amount function to each row in the final_df DataFrame, and the function will fill the appropriate tax amount based on the value in the Tax_Term column. The axis=1 argument specifies that the function should be applied to each row rather than each column.

Alternatively, you can use the pivot_table method to achieve the same result:

final_df = final_df.pivot_table(index=['Rate', 'TDS', 'Tax_Term'], 
                                columns='Tax_Term', 
                                values='Tax_Amount', 
                                aggfunc='first').reset_index()
Answered By: testing0s

If there are few values in the Tax_Term column, you can simply use a series of assignments using .loc:

final_df.loc[final_df['Tax_Term'] == 'CGST','CGST'] =final_df['Tax_Ammount']
final_df.loc[final_df['Tax_Term'] == 'TDS', 'TDS'] = final_df['Tax_Ammount']
final_df.loc[final_df['Tax_Term'] == 'SGST', 'SGST'] = final_df['Tax_Ammount']
final_df.loc[final_df['Tax_Term'] == 'IGST', 'IGST'] = final_df['Tax_Ammount']

Or, you can create a loop that iterates over unique values in Tax_term:

for term in final_df.Tax_Term.unique():
    final_df.loc[final_df['Tax_Term'] == term,
                 term] = final_df['Tax_Ammount']
Answered By: Oliver