[英]Replace multiple column value based on multiple columns in pandas Dataframe
A bit of your help will be highly appreciated.您的一些帮助将不胜感激。 I have a dataframe like below:
我有一个 dataframe 如下所示:
name City kpi 01-Jan-20 02-Jan-20 03-Jan-20 SD Mean
1 A X 9 9 9 0 0 9
1 A Y 120 120 120 0 0 120
2 A X 10 20 30 10 10 20
2 A Y 1 0 11 6.08276253 4
3 B X 1 2 3 1 2
3 B Y 2 100 5 55.73448962 35.66666667
I want to have an output based on following methods, which takes kpi values for all days(I have 100 days in real df).我想有一个基于以下方法的 output,它采用所有天的 kpi 值(我在实际 df 中有 100 天)。
def indicator(kpi_value, kpi_std, kpi_mean ):
SD_distance = abs((kpi_mean-kpi_value)/kpi_std)
if SD_distance >=1.5:
return -1
else:
return 1
The final output should be like this:最终的 output 应该是这样的:
name City kpi 01-Jan-20 02-Jan-20 03-Jan-20 SD
1 A X 1 or -1 1 or -1 1 or -1 0
1 A Y 1 or -1 1 or -1 1 or -1 0
2 A X 1 or -1 1 or -1 1 or -1 12.41739671
2 A Y 1 or -1 1 or -1 1 or -1 0.016969928
3 B X 1 or -1 1 or -1 1 or -1 0.013699487
3 B Y 1 or -1 1 or -1 1 or -1 0
I tried with different ways, but could not succeed.我尝试了不同的方法,但无法成功。
date_cols = df_new.iloc[:,3:-2].columns
df_new[date_cols[0]] = df_new.apply(lambda x: indicator(x[date_cols[0]],x["SD"],x["Mean"]))
Would be glad to have any suggestions.很高兴有任何建议。 Thank you!
谢谢!
I think the issue is that you are missing the axis=1
parameter for column-wise in the .apply()
function.我认为问题在于您在
.apply()
function 中缺少按列的axis=1
参数。
date_cols = df_new.iloc[:,3:-2].columns
for col in date_cols:
df_new[col] = df_new.apply(lambda x: indicator(x[col], x["SD"], x["Mean"]), axis=1)
print(df_new)
Result:结果:
name city kpi 01-Jan-20 02-Jan-20 03-Jan-20 SD Mean
0 1 A X 1 1 -1 0.000000 9.000000
1 1 A Y 1 1 -1 0.000000 120.000000
2 2 A X 1 1 1 10.000000 20.000000
3 2 A Y 1 1 1 6.082763 4.000000
4 3 B X 1 1 1 1.000000 2.000000
5 3 B Y 1 1 1 55.734490 35.666667
Note - In your indicator
function, the statement SD_distance = abs((kpi_mean-kpi_value)/kpi_std)
will give you error if kpi_std
is 0. You might want to revisit that logic.注意 - 在您的
indicator
function 中,如果kpi_std
为 0,语句SD_distance = abs((kpi_mean-kpi_value)/kpi_std)
将给您错误。您可能需要重新审视该逻辑。 Also how did you arrive at the value of SD
in your expected result?另外,您是如何得出预期结果中的
SD
值的? Is there a logic for it?有逻辑吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.