簡體   English   中英

根據特定條件將 NaN 替換為其他列中的值

[英]Replace NaN with values from other column based on certain conditions

我有一個帶有多個索引(編號和類型)的數據集,如下所示,我想用來自 l2 的 R1 值替換 Node1 和 Node2 類型的列 l2 中的 NaN 值,並用 l2 值替換 Node3 和 Node4 類型的 NaN 值R2。 如何在 pandas 中執行此操作?

    name    l1          l2
No. type        
1   Node1   41.656123   NaN
    Node2   95.232711   NaN
    Node3   41.660935   NaN
    Node4   95.144500   NaN
    R1       NaN    0.000144
    R2       NaN    0.000154
2   Node1   41.656142   NaN
    Node2   95.232730   NaN
    Node3   41.660957   NaN
    Node4   95.144525   NaN
    R1       NaN    0.000200
    R2       NaN    0.000232

預期結果應如下所示:

    name    l1          l2
No. type        
1   Node1   41.656123   0.000144
    Node2   95.232711   0.000144
    Node3   41.660935   0.000154
    Node4   95.144500   0.000154
    R1       NaN    0.000144
    R2       NaN    0.000154
2   Node1   41.656142   0.000200
    Node2   95.232730   0.000200
    Node3   41.660957   0.000232
    Node4   95.144525   0.000232
    R1       NaN    0.000200
    R2       NaN    0.000232

提取typeR1R2的 dataframe 並將 R1 和 R2 分別替換為 Node1 和 Node 2

df1=df.query('type == ["R2", "R1"]').reset_index()f#filter Rs to be renamed as Nodes for purposes of joining down the line
df3=df.query('type == ["R2", "R1"]').reset_index()#.set_index('No.')# filter of Rs not to be renamed but to be reappended later
df1.replace(['R1','R2'], ['Node1','Node3'], inplace=True)

刪除l2因為你在這里不需要它,它有NaNs和重置索引

df1.drop(columns=['l1'], inplace=True)
df1.set_index(['No.','type'], inplace=True)
df1

提取 dataframe type不等於R1R2

df2=df.query('type != ["R2", "R1"]').reset_index()#.set_index('No.')

刪除l2因為你在這里不需要它,它有NaNs和重置索引

df2.drop(columns=['l2'], inplace=True)
df2.set_index(['No.','type'], inplace=True)
df2

合並兩個數據框

df4=df1.merge(df2, left_index=True, right_index=True, how='outer').ffill()

回調Rs的過濾器並設置索引以符合df4

df3.set_index(['No.','type'], inplace=True)
df3

Append df3df4並按索引排序

final=df4.append(df3).sort_index()
final

Output

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM