【问题标题】:Take difference between two column of pandas dataframe based on condition in python根据python中的条件取两列熊猫数据框之间的差异
【发布时间】:2016-08-03 23:11:10
【问题描述】:

我有一个名为 pricecomp_df 的数据框,我想比较“市场价格”列的价格和“苹果价格”、“芒果价格”、“西瓜价格”等其他列的价格,但优先考虑差异条件:(西瓜价格第一,芒果第二,苹果第三)。输入数据框如下:

   code  apple price  mangoes price  watermelon price  market price
0   101          101            NaN               NaN           122
1   102          123            123               NaN           124
2   103          NaN            NaN               NaN           123
3   105          123            167               NaN           154
4   107          165            NaN               177           176
5   110          123            NaN               NaN           123

所以这里第一行只有苹果价格和市场价格,然后取它们的差异,但在第二行,我们有苹果和芒果的价格,所以我只需要取市场价格和芒果价格之间的差额。同样根据优先条件取差值。对于所有三个价格,也跳过带有 nan 的行。有人可以帮忙吗?

【问题讨论】:

    标签: python conditional diff


    【解决方案1】:

    希望我不会太晚。这个想法是计算差异并根据您的优先级列表覆盖它们。

    import numpy as np
    import pandas as pd
    
    df = pd.DataFrame({'code': [101, 102, 103, 105, 107, 110],
                       'apple price': [101, 123, np.nan, 123, 165, 123],
                       'mangoes price': [np.nan, 123, np.nan, 167, np.nan, np.nan],
                       'watermelon price': [np.nan, np.nan, np.nan, np.nan, 177, np.nan],
                       'market price': [122, 124, 123, 154, 176, 123]})
    
    # Calculate difference to apple price
    df['diff'] = df['market price'] - df['apple price']
    # Overwrite with difference to mangoes price
    df['diff'] = df.apply(lambda x: x['market price'] - x['mangoes price'] if not np.isnan(x['mangoes price']) else x['diff'], axis=1)
    # Overwrite with difference to watermelon price
    df['diff'] = df.apply(lambda x: x['market price'] - x['watermelon price'] if not np.isnan(x['watermelon price']) else x['diff'], axis=1)
    
    print df
       apple price  code  mangoes price  market price  watermelon price  diff
    0          101   101            NaN           122               NaN    21
    1          123   102            123           124               NaN     1
    2          NaN   103            NaN           123               NaN   NaN
    3          123   105            167           154               NaN   -13
    4          165   107            NaN           176               177    -1
    5          123   110            NaN           123               NaN     0
    

    【讨论】:

      猜你喜欢
      • 2016-07-22
      • 2018-04-18
      • 1970-01-01
      • 1970-01-01
      • 2018-06-27
      • 2022-10-02
      • 1970-01-01
      • 2021-06-18
      • 1970-01-01
      相关资源
      最近更新 更多