[英]Pandas how do I prevent myself from iterating through rows?
I have the following dataframe我有以下 dataframe
Charge_type Amount Quantity
Credit 2.5 3
Credit 3.24 2
Debit 5.98 6
I need the following output, if Charge_type is a 'credit' then multiply 'Amount' * -1 and 'Quantity' * -1 to get the following result:我需要以下 output,如果 Charge_type 是“信用”,则将“金额”* -1 和“数量”* -1 相乘以获得以下结果:
Charge_type Amount Quantity
Credit -2.5 -3
Credit -3.24 -2
Debit 5.98 6
I have no idea where to begin, everywhere I looked online says not to iterate in pandas.我不知道从哪里开始,我在网上看到的所有地方都说不要在 pandas 中迭代。 Sorry I'm still such a beginner with Python and Pandas.抱歉,我仍然是 Python 和 Pandas 的初学者。 Any help is appreciated.任何帮助表示赞赏。
One option is to use loc
:一种选择是使用loc
:
df.loc[df['Charge_type'].eq('Credit'), ['Amount', 'Quantity']] *= -1
The first part creates a Boolean index df['Charge_type'].eq('Credit')
(to select rows based on where Charge_type is 'Credit').第一部分创建 Boolean 索引df['Charge_type'].eq('Credit')
(根据 Charge_type 为 'Credit' 的位置到 select 行)。
The second part choses the columns to affect ['Amount', 'Quantity']
in this case, 'Amount' and 'Quantity'.第二部分选择影响['Amount', 'Quantity']
的列,在这种情况下,'Amount'和'Quantity'。
Lastly, mutate using the shortcut operator *=
to negate the values.最后,使用快捷运算符*=
进行变异以否定值。
df
: df
:
Charge_type Amount Quantity
0 Credit -2.50 -3
1 Credit -3.24 -2
2 Debit 5.98 6
You can create a masking for the column Charge_type
, and use .loc
along with column name to access the column where the masking has True
value, and then assign the new value accordingly:您可以为Charge_type
列创建掩码,并使用.loc
和列名来访问掩码具有True
值的列,然后相应地分配新值:
mask = df['Charge_type'] == 'Credit'
df.loc[mask, 'Amount'] = -df['Amount']
df.loc[mask, 'Quantity'] = -df['Quantity']
OUTPUT : OUTPUT :
Charge_type Amount Quantity
0 Credit -2.50 -3
1 Credit -3.24 -2
2 Debit 5.98 6
I think combining the approaches from the other two answers (already upvoted both) is most readable:我认为结合其他两个答案的方法(已经赞成)是最易读的:
mask = df['Charge_type'] == 'Credit'
df.loc[mask, ['Amount', 'Quantity']] *= -1
Output: Output:
Charge_type Amount Quantity
0 Credit -2.50 -3
1 Credit -3.24 -2
2 Debit 5.98 6
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.