1

我有以下df:

subscription|amount| Total
a           |500   |
x           | 0    | 5000
x           |7500  | 5000
y           |7500  | 5000
y           | 0    | 5000
z           |7500  | 5000
z           | 0    | 5000
b           | 0    | 
b           |1000  |

我想将“总计”的值转移到“金额”中已经有一个值的“金额”列,当然还有“总计”。

我过滤了:

df.loc[(df['total'].notnull()) & (df['amount']!=0)]

但无法将值从总计(5000)转移到金额。

期望的输出:

subscription|amount| Total
a           |500   |
x           | 0    | 5000
x           |5000  | 5000
y           |5000  | 5000
y           | 0    | 5000
z           |5000  | 5000
z           | 0    | 5000
b           | 0    | 
b           |1000  |
4

1 回答 1

1

如果将列更改为浮点数没有问题,还有更多解决方案 -DataFrame.locnumpy.where

mask = (df['Total'].notnull()) & (df['amount']!=0)

df.loc[mask, 'amount'] = df['Total']

df['amount'] = np.where(mask, df['Total'], df['amount'])

print (df)
  subscription  amount   Total
0            a   500.0     NaN
1            x     0.0  5000.0
2            x  5000.0  5000.0
3            y  5000.0  5000.0
4            y     0.0  5000.0
5            z  5000.0  5000.0
6            z     0.0  5000.0
7            b     0.0     NaN
8            b  1000.0     NaN

没有将整数列更改为浮点数或转换为with 的Series.mask解决方案:Series.whereintegersnp.where

df['amount'] = df['amount'].mask(mask, df['Total'])

df['amount'] = df['amount'].where(~mask, df['Total'])

df['amount'] = df['amount'].where(~mask, df['Total']).astype(int)

print (df)
  subscription  amount   Total
0            a     500     NaN
1            x       0  5000.0
2            x    5000  5000.0
3            y    5000  5000.0
4            y       0  5000.0
5            z    5000  5000.0
6            z       0  5000.0
7            b       0     NaN
8            b    1000     NaN
于 2019-04-02T12:06:42.373 回答