简体   繁体   English

根据 pandas Dataframe 中的多列替换多列值

[英]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.

相关问题 基于行和多列的pandas dataframe列 - pandas dataframe column based on row and multiple columns 根据Pandas中的多个条件替换列中的值 - Replace value in column based on multiple conditions in Pandas 熊猫通过将数据框列与其他多个列进行匹配来生成列 - Pandas generates a column based by matching the dataframe columns to multiple other columns Pandas数据框:根据另一列中的值替换多行 - Pandas dataframe: Replace multiple rows based on values in another column pandas DataFrame:用另一个值替换多个列中的值 - pandas DataFrame: replace values in multiple columns with the value from another 如何根据 pandas 中多列的条件替换列中的值 - How to replace values in a column based on conditions from multiple columns in pandas 通过根据列值熊猫数据框将一列置于另一列之下,将多列合并为一列 - Merge multiple columns into one by placing one below the other based on column value pandas dataframe 根据pandas DataFrame中的列值有条件地替换多个列 - Conditional replacement of multiple columns based on column values in pandas DataFrame 根据Pandas DataFrame中单个列中的值创建多个列 - Create multiple columns based on values in single column in Pandas DataFrame 向 Pandas DataFrame 添加一列,并基于其他列进行多次查找 - Add a column to Pandas DataFrame with multiple lookups based on other columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM