我想计算一个分类变量的均值。示例如下:
dt = {"var1": ["1-2", "3-4","6-8","Missing"],
"Wanted result": ["1.5", "3.5","7","Missing"]}
pd.DataFrame(dt)
var1 Wanted result
0 1-2 1.5
1 3-4 3.5
2 6-8 7
3 Missing Missing
怎样才能从"var1“中得到”想要的结果“?
发布于 2020-07-14 08:41:49
您可以使用to_numeric
执行split
操作
s=df.var1.str.split('-',expand=True).apply(pd.to_numeric,errors='coerce').mean(axis=1).fillna(df.var1)
0 1.5
1 3.5
2 7
3 Missing
dtype: object
df['wanted']=s
https://stackoverflow.com/questions/62886372
复制相似问题