简体   繁体   English

如何将另一行中的NaN替换为另一行中的值

[英]How Can I replace NaN in a row with values in another row Pandas

I tried several methods to replace NaN in a row with values in another row, but none of them worked as expected. 我尝试了几种用另一行中的值替换行中的NaN的方法,但是它们均未按预期工作。 Here is my Dataframe: 这是我的数据框:

test = pd.DataFrame(
    {
        "a": [1, 2, 3, 4, 5], 
        "b": [4, 5, 6, np.nan, np.nan], 
        "c": [7, 8, 9, np.nan, np.nan], 
        "d": [7, 8, 9, np.nan, np.nan]
     }
)

   a    b    c    d
0  1   4.0  7.0  7.0
1  2   5.0  8.0  8.0
2  3   6.0  9.0  9.0
3  4   NaN  NaN  NaN
4  5   NaN  NaN  NaN

I need to replace NaN in 4th row with values first row, ie, 我需要将第四行的NaN替换为第一行的值,即

   a     b     c     d
0  1   **4.0   7.0   7.0**
1  2    5.0   8.0   8.0
2  3    6.0   9.0   9.0
3  4   **4.0   7.0   7.0**
4  5    NaN   NaN   NaN

And the second question is how can I multiply some/part values in a row by a number, for example, I need to double the values in second two when the columns are ['b', 'c', 'd'] , then the result is: 第二个问题是如何将行中的某些/部分值乘以数字,例如,当列为['b', 'c', 'd']时,我需要将第二个中的值加倍。那么结果是:

   a     b     c     d
0  1    4.0   7.0   7.0
1  2   **10.0  16.0  16.0**
2  3    6.0   9.0   9.0
3  4    NaN   NaN   NaN
4  5    NaN   NaN   NaN

First of all, I suggest you do some reading on Indexing and selecting data in pandas. 首先,我建议您阅读有关索引和选择熊猫中数据的内容。 Regaring the first question you can use .loc() with isnull() to perform boolean indexing on the column vaulues: 关于第一个问题,您可以将.loc()isnull()以对列值执行布尔索引:

mask_nans = test.loc[3,:].isnull()
test.loc[3, mask_nans] = test.loc[0, mask_nans]

And to double the values you can directly multiply by 2 the sliced dataframe also using .loc() : .loc()值翻倍,您还可以使用.loc()将切片的数据帧直接乘以2

test.loc[1,'b':] *= 2

   a     b     c     d
0  1   4.0   7.0   7.0
1  2  10.0  16.0  16.0
2  3   6.0   9.0   9.0
3  4   4.0   7.0   7.0
4  5   NaN   NaN   NaN

Indexing with labels 用标签索引

If you wish to filter by a , and a values are unique, consider making it your index to simplify your logic and make it more efficient: 如果你想通过筛选a ,和a值是唯一的,可以考虑将它的索引,以简化你的逻辑,并使其更有效率:

test = test.set_index('a')
test.loc[4] = test.loc[4].fillna(test.loc[1])
test.loc[2] *= 2

Boolean masks 布尔掩码

If a is not unique and Boolean masks are required, you can still use fillna with an additional step:: 如果a不是唯一的并且需要布尔掩码,则仍然可以使用fillna并执行以下附加步骤:

mask = test['a'].eq(4)
test.loc[mask] = test.loc[mask].fillna(test.loc[test['a'].eq(1).idxmax()])
test.loc[test['a'].eq(2)] *= 2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM