如何根据条件将一列的值设置为另一列
how to set the value from a column to another based on a condition
我有以下 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 |
我想将 'total' 的值转移到 'amount',其中 'amount' 列中已经有值,当然 'total' 中也有值。
我过滤了:
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 |
还有更多的解决办法,如果column改成float没问题- DataFrame.loc
or numpy.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
没有将整数列更改为使用 Series.mask
, Series.where
浮动或使用 np.where
转换为 integers
的解决方案:
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
我有以下 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 |
我想将 'total' 的值转移到 'amount',其中 'amount' 列中已经有值,当然 'total' 中也有值。
我过滤了:
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 |
还有更多的解决办法,如果column改成float没问题- DataFrame.loc
or numpy.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
没有将整数列更改为使用 Series.mask
, Series.where
浮动或使用 np.where
转换为 integers
的解决方案:
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