pandas 如何在python中基于row after的条件填充特定列中的字符串缺失值?

rvpgvaaj  于 11个月前  发布在  Python
关注(0)|答案(1)|浏览(100)

我想在python.enter image description here中基于行后的条件在一列中填充字符串缺失值
缺少的值应该与后面的行有不同的值(如果我们从及时的Angular 来看),所以它应该是connect -> disconnect -> connect等等。所以在这种情况下,我想让Monitoring Desc缺少的值用“disconnect”填充,因为后面的行是“connect”。
我试过了:df['Monitoring Desc'] = df['Monitoring Desc'].fillna(np.where(df['Monitoring Desc'].shift(1) == "ws_connect", "ws_disconnect", "ws_connect"))
但显示错误消息:TypeError:“value”参数必须是标量,dict或Series,但您传递了“ndarray”
我没有在python中做这种分析,所以我想不出其他的东西。也许有什么与循环或函数有关的东西来填充这些缺失的值?任何帮助都将不胜感激。

igsr9ssn

igsr9ssn1#

验证码

plz提供输入作为文本表不图像。
Map反补

m = {'ws_connect':'ws_disconnect', 'ws_disconnect':'ws_connect'}
df['Monitoring Desc'] = df['Monitoring Desc'].fillna(df['Monitoring Desc'].map(m).ffill())

字符串
DF

Monitoring Desc datetime
3956    ws_disconnect   2023-11-01 21:00
3955    ws_disconnect   2023-11-01 20:00
1336    ws_disconnect   2023-11-01 19:58
3954    ws_connect      2023-11-01 19:00
1363    ws_connect      2023-11-01 18:00
1364    ws_disconnect   2023-11-01 18:00
1365    ws_connect      2023-11-01 18:00
3953    ws_disconnect   2023-11-01 18:00
3952    ws_disconnect   2023-11-01 17:00
3951    ws_disconnect   2023-11-01 16:00
3950    ws_disconnect   2023-11-01 15:00
3949    ws_disconnect   2023-11-01 14:00

示例代码

import pandas as pd
data1 = {'Monitoring Desc': {3956: 'ws_disconnect', 3955: 'ws_disconnect', 1336: 'ws_disconnect', 
                             3954: 'ws_connect', 1363: 'ws_connect', 1364: 'ws_disconnect', 
                             1365: 'ws_connect', 3953: 'ws_disconnect', 3952: 'ws_disconnect', 
                             3951: 'ws_disconnect', 3950: 'ws_disconnect', 3949: 'ws_disconnect'}, 
         'datetime': {3956: '2023-11-01 21:00', 3955: '2023-11-01 20:00', 1336: '2023-11-01 19:58', 
                      3954: '2023-11-01 19:00', 1363: '2023-11-01 18:00', 1364: '2023-11-01 18:00', 
                      1365: '2023-11-01 18:00', 3953: '2023-11-01 18:00', 3952: '2023-11-01 17:00', 
                      3951: '2023-11-01 16:00', 3950: '2023-11-01 15:00', 3949: '2023-11-01 14:00'}}
df = pd.DataFrame(data1)

相关问题