![](/img/trans.png)
[英]Pandas: Iterating over rows, Adding and Subtracting Dates, Appending those dates to a new column depending on row value
[英]Pandas dataframe: Manipulate column depending on value in another column (without iterating over rows)
在不循環/迭代行的情況下,我想操作一個數據框,以便根據一列(Transaction_Type)中的值,另一列(Gross_Value)中的值乘以 -1 或不乘。
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
對於所有銷售信用,Gross_Value 應該用負數表示:
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
謝謝!
使用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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.