Without looping/iterating over rows I would like to manipulate a dataframe so that depending on the value in one column (Transaction_Type) the value in another column (Gross_Value) gets multiplied by -1 or not.
Transaction_# Transaction_Type Gross_Value
5542-6990 Invoice 39.00
5981-3808 SalesCredit 89.00
8058-9885 Invoice 199.00
5420-6262 SalesCredit 99.00
For all sales credits the Gross_Value should be represented with the negative number:
Transaction_# Transaction_Type Gross_Value
5542-6990 Invoice 39.00
5981-3808 SalesCredit -89.00
8058-9885 Invoice 199.00
5420-6262 SalesCredit -99.00
Thanks!
Use loc
m = (df['Transaction_Type'] == 'SalesCredit')
df.loc[m, 'Gross_Value'] *= -1
Transaction_# Transaction_Type Gross_Value
0 5542-6990 Invoice 39.0
1 5981-3808 SalesCredit -89.0
2 8058-9885 Invoice 199.0
3 5420-6262 SalesCredit -99.0
尝试这个
df.loc[df['Transaction_Type'] == 'Sales Credit','Gross_Value'] = df['Gross_Value'] * -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.