您可以将价值替换为<代码>。 正确的s in Quality_Start
and Quality_End
to Disappearance first, here in Series. where
:
g = (df.assign(Abs_s=df[ Abs_s ].where(df[ Quality_Start ]),
Abs_e=df[ Abs_e ].where(df[ Quality_End ]))
.groupby(["Part", "Rel_s"]))
df[ Abs_s ] = g[ Abs_s ].transform( min )
df[ Abs_e ] = g[ Abs_e ].transform( max )
print (df)
Name Part Rel_s Rel_e Abs_s Abs_e Quality_Start Quality_End
0 PF2 E1 -500 -127 199655.0 200564 True True
1 PR2 E1 -500 -167 199655.0 200324 True True
2 PF2 E1 -500 -167 199655.0 200324 False True
3 PF2 E1 -400 -127 199155.0 200564 True True
4 PR2 E1 -400 -167 199155.0 200324 True True
www.un.org/spanish/ecosoc 如何工作:
print (df.assign(Abs_s=df[ Abs_s ].where(df[ Quality_Start ]),
Abs_e=df[ Abs_e ].where(df[ Quality_End ])))
Name Part Rel_s Rel_e Abs_s Abs_e Quality_Start Quality_End
0 PF2 E1 -500 -127 199971.0 200164 True True
1 PR2 E1 -500 -167 199655.0 200124 True True
2 PF2 E1 -500 -167 NaN 200124 False True
3 PF2 E1 -400 -127 199971.0 200564 True True
4 PR2 E1 -400 -167 199155.0 200324 True True
如果需要电离层的分类,可按<代码>将具有缺失值的一栏转换成。 Int64 :
g = (df.assign(Abs_s=df[ Abs_s ].where(df[ Quality_Start ]).astype( Int64 ),
Abs_e=df[ Abs_e ].where(df[ Quality_End ]).astype( Int64 ))
.groupby(["Part", "Rel_s"]))
df[ Abs_s ] = g[ Abs_s ].transform( min )
df[ Abs_e ] = g[ Abs_e ].transform( max )
print (df)
Name Part Rel_s Rel_e Abs_s Abs_e Quality_Start Quality_End
0 PF2 E1 -500 -127 199655 200164 True True
1 PR2 E1 -500 -167 199655 200164 True True
2 PF2 E1 -500 -167 199655 200164 False True
3 PF2 E1 -400 -127 199155 200564 True True
4 PR2 E1 -400 -167 199155 200564 True True
www.un.org/spanish/ecosoc 如何工作:
print (df.assign(Abs_s=df[ Abs_s ].where(df[ Quality_Start ]).astype( Int64 ),
Abs_e=df[ Abs_e ].where(df[ Quality_End ]).astype( Int64 )))
Name Part Rel_s Rel_e Abs_s Abs_e Quality_Start Quality_End
0 PF2 E1 -500 -127 199655 200164 True True
1 PR2 E1 -500 -167 199655 200164 True True
2 PF2 E1 -500 -167 <NA> 200164 False True
3 PF2 E1 -400 -127 199155 200564 True True
4 PR2 E1 -400 -167 199155 200564 True True
a. 测试,不匹配:
lst = [[ PF2 , E1 , -500, -127, 199971, 200164, False, True],
[ PR2 , E1 , -500, -167, 199655, 200124, False, True],
[ PF2 , E1 , -500, -167, 199645, 200124, False, True],
[ PF2 , E1 , -400, -127, 199971, 200564, True, True],
[ PR2 , E1 , -400, -167, 199155, 200324, True, True]]
df = pd.DataFrame(lst, columns=["Name", "Part", "Rel_s",
"Rel_e", "Abs_s", "Abs_e",
"Quality_Start", "Quality_End"])
g = (df.assign(Abs_s=df[ Abs_s ].where(df[ Quality_Start ]).astype( Int64 ),
Abs_e=df[ Abs_e ].where(df[ Quality_End ]).astype( Int64 ))
.groupby(["Part", "Rel_s"]))
df[ Abs_s ] = g[ Abs_s ].transform( min )
df[ Abs_e ] = g[ Abs_e ].transform( max )
print (df)
Name Part Rel_s Rel_e Abs_s Abs_e Quality_Start Quality_End
0 PF2 E1 -500 -127 <NA> 200164 False True
1 PR2 E1 -500 -167 <NA> 200164 False True
2 PF2 E1 -500 -167 <NA> 200164 False True
3 PF2 E1 -400 -127 199155 200564 True True
4 PR2 E1 -400 -167 199155 200564 True True
print (df.assign(Abs_s=df[ Abs_s ].where(df[ Quality_Start ]).astype( Int64 ),
Abs_e=df[ Abs_e ].where(df[ Quality_End ]).astype( Int64 )))
Name Part Rel_s Rel_e Abs_s Abs_e Quality_Start Quality_End
0 PF2 E1 -500 -127 <NA> 200164 False True
1 PR2 E1 -500 -167 <NA> 200164 False True
2 PF2 E1 -500 -167 <NA> 200164 False True
3 PF2 E1 -400 -127 199155 200564 True True
4 PR2 E1 -400 -167 199155 200564 True True
EDIT:
#one groupby with created 2 temporary columns
g = (df.assign(Abs_s_t=df[ Abs_s ].where(df[ Quality_Start ]).astype( Int64 ),
Abs_e_t=df[ Abs_e ].where(df[ Quality_End ]).astype( Int64 ))
.groupby(["Part", "Rel_s"]))
#if need replace by original values
df[ Abs_s ] = g[ Abs_s_t ].transform( min ).fillna(df[ Abs_s ])
df[ Abs_e ] = g[ Abs_e_t ].transform( max ).fillna(df[ Abs_e ])
#if need replace by min/max values per groups
df[ Abs_s1 ] = g[ Abs_s_t ].transform( min ).fillna(g[ Abs_s ].transform( min ))
df[ Abs_e1 ] = g[ Abs_e_t ].transform( max ).fillna(g[ Abs_e ].transform( max ))
print (df)
Name Part Rel_s Rel_e Abs_s Abs_e Quality_Start Quality_End Abs_s1 Abs_e1
0 PF2 E1 -500 -127 199971 200164 False True 199645 200164
1 PR2 E1 -500 -167 199655 200164 False True 199645 200164
2 PF2 E1 -500 -167 199645 200164 False True 199645 200164
3 PF2 E1 -400 -127 199155 200564 True True 199155 200564
4 PR2 E1 -400 -167 199155 200564 True True 199155 200564