仅当满足另一列的条件时填充na
我有一个看起来像这样的dataframe 测试:
| sales |transactions|
|-------|------------|
|0.0 |NaN |
|0.0 |NaN |
|3802.29|NaN |
|4520.35|8359 |
我正在寻找一种在“销售”列中仅具有0的行中填充NAN值的方法,而无需更改其他行。我尝试了一下:
test['transactions'] = test.apply(
lambda row: 0 if row['sales'] == 0 else None,
axis=1)
它适用于那些行,但问题是填充了NAN所有其他行
输出:
| sales |transactions|
|-------|------------|
|0.0 |0.0 |
|0.0 |0.0 |
|3802.29|NaN |
|4520.35|NaN |
预期结果:
| sales |transactions|
|-------|------------|
|0.0 |0.0 |
|0.0 |0.0 |
|3802.29|NaN |
|4520.35|8359 |
预先感谢您。
I have a dataframe test that looks like this:
| sales |transactions|
|-------|------------|
|0.0 |NaN |
|0.0 |NaN |
|3802.29|NaN |
|4520.35|8359 |
I'm looking for a way to fill the NaN values with 0 of only the rows that have 0 in the 'sales' column, without changing the other rows. I tried this:
test['transactions'] = test.apply(
lambda row: 0 if row['sales'] == 0 else None,
axis=1)
It works for those rows but the problem is that fills with NaN all the other rows
Output:
| sales |transactions|
|-------|------------|
|0.0 |0.0 |
|0.0 |0.0 |
|3802.29|NaN |
|4520.35|NaN |
Expected result:
| sales |transactions|
|-------|------------|
|0.0 |0.0 |
|0.0 |0.0 |
|3802.29|NaN |
|4520.35|8359 |
Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
mask
具体来说,在
mask
中使用其他
参数,如果您有一项交易,在销售额为零的情况下不是零,并且不要想要用零替换非无效事务,然后做:
mask
Specifically, use the
other
argument inmask
In the event you have a transaction that isn't null where sales are zero and don't want to replace a non-null transaction with zero then do: