【问题标题】:Conditional statement / If statement with Dataframes带有数据框的条件语句/If 语句
【发布时间】:2021-06-19 08:24:30
【问题描述】:

我正在尝试根据多个列“类”和“值”为“百分比”列分配一个值

以下是包含我的数据框的链接: https://filebin.net/fo2wk7crmwf0fycc

这是我要应用的逻辑:

If df['Class'] equals 2 or 3, and if df['Value'] is less than 0.5, set df['Percentage'] to 0
If df['Class'] equals 2 or 3, and if df['Value'] is > 0.5 and <= 0.7, set df['Percentage'] to 0.25
If df['Class'] equals 2 or 3, and if df['Value'] is > 0.7 and <= 0.9, set df['Percentage'] to 0.5
Else set df['Percentage'] to 1

下面是我正在寻找的输出示例:

Class Value Percentage
2 0.01 0
2 0.6 0.25
3 0.9 0.5
3 3 1

谢谢

【问题讨论】:

    标签: python pandas dataframe if-statement conditional-statements


    【解决方案1】:

    Numpy 和 searchsorted

    使用searchsorted 时,在这种情况下,您不需要包含01 之类的边界。

    bins =  np.array([.5, .7, .9])
    labels = np.array([0, .25, .5, 1])
    cut = bins.searchsorted(df.Value)
    results = labels[cut]
    
    df.assign(Percentage=np.where(df['Class'].isin([2, 3]), results, 1))
    
           Class     Value  Percentage
    0          2  0.000620         0.0
    1          2  0.000620         0.0
    2          3  0.001240         0.0
    3          4  0.000620         1.0
    4          5  0.000620         1.0
    ...      ...       ...         ...
    14782      5  0.001178         1.0
    14783      2  0.001116         0.0
    14784      3  0.001178         0.0
    14785      5  0.000310         1.0
    14786      5  0.001116         1.0
    
    [14787 rows x 3 columns]
    

    熊猫cut

    使用pd.cut 时,您确实需要边界,因为 Pandas 会创建间隔。

    #                        / boundaries \
    #                       ↓              ↓
    cut = pd.cut(df.Value, [0, .5, .7, .9, 1], labels=[0, .25, .5, 1])
    
    df.assign(Percentage=np.where(df['Class'].isin([2, 3]), cut, 1))
    
           Class     Value  Percentage
    0          2  0.000620         0.0
    1          2  0.000620         0.0
    2          3  0.001240         0.0
    3          4  0.000620         1.0
    4          5  0.000620         1.0
    ...      ...       ...         ...
    14782      5  0.001178         1.0
    14783      2  0.001116         0.0
    14784      3  0.001178         0.0
    14785      5  0.000310         1.0
    14786      5  0.001116         1.0
    
    [14787 rows x 3 columns]
    

    【讨论】:

      【解决方案2】:

      你也可以像下面这样使用纯np.where

      import numpy as np    
      df['Percentage'] = np.where((df['Class'].isin([2, 3]) & (df['Value'] <= 0.5)), 0, 
                                  np.where((df['Class'].isin([2, 3]) & (df['Value'] > 0.5) & (df['Value'] <= 0.7)), 0.25,
                                      np.where((df['Class'].isin([2, 3]) & (df['Value'] > 0.7) & (df['Value'] <= 0.9) ), 0.5, 1)))
      

      np.where 就像 if-then-else 条件语句,你很容易理解。

             Class     Value  Percentage
      0          2  0.000620         0.0
      1          2  0.000620         0.0
      2          3  0.001240         0.0
      3          4  0.000620         1.0
      4          5  0.000620         1.0
      ...      ...       ...         ...
      14782      5  0.001178         1.0
      14783      2  0.001116         0.0
      14784      3  0.001178         0.0
      14785      5  0.000310         1.0
      14786      5  0.001116         1.0
      
      [14787 rows x 3 columns]
      

      【讨论】:

      • 非常感谢,这个解决方案完美运行!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-15
      • 2017-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多