简体   繁体   中英

Replace NaN with values from other column based on certain conditions

I have a data set looks like the following with multi indices (No. and type) and want to replace NaN value in column l2 of type Node1 and Node2 with R1 value from l2 and replace NaN values of type Node3 and Node4 with l2 values of R2. How can I do this in 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

The expected outcome should look like:

    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

Extract dataframe with type that is eq to either R1 or R2 and replace the R1 and R2 with Node1 and Node 2 respectively

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)

Drop l2 beacuse you do not need it here, it has NaNs and reset index

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

Extract dataframe with type that is not eq to either R1 or R2

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

Drop l2 beacuse you do not need it here, it has NaNs and reset index

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

Merge the two dataframes

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

Call back a fillter of the Rs and set index to conform to the df4

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

Append df3 to df4 and sort by index

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

Output

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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