如果我有几列
readarray -d '' arr < <(cmd | gawk -v RS='"[^"]*"' 'RT { gsub("\"", "", RT); printf RT"\0"}')
如果“交易类型”为借方?
,如何迭代列以使金额变为负数Transaction Type Amount
debit 1
credit 1
答案 0 :(得分:3)
使用.loc
df.loc[df['TransactionType']=='debit','Amount']=-df.Amount
df
Out[376]:
TransactionType Amount
0 debit -1
1 credit 1
答案 1 :(得分:3)
(-1) ** True
为我们提供了-1
,(-1) ** False
为我们提供了1
:
In [105]: df.Amount *= (-1) ** (df['Transaction Type'].eq('debit'))
In [106]: df
Out[106]:
Transaction Type Amount
0 debit -1
1 credit 1
答案 2 :(得分:2)
你想要的
df.Amount[df['Transaction Type']=='debit'] = - df.Amount