[英]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.