如何用熊猫中的条件替换两列?

时间:2020-04-21 00:52:08

标签: python python-3.x pandas lambda

我有一个Pandas DataFrame,其中包含产品,动作,数量和价格。如果操作为“卖”,则我需要考虑产品的价格0并求反。

我尝试过的事情:

import pandas as pd


def my_function(df):
    kwargs = {
        "quantity": lambda x: -x["quantity"] if "SELL" in str(x["action"]) else x["quantity"],
        "price (usd)": lambda x: 0 if "SELL" in str(x["action"]) else x["price (usd)"],
    }
    return df.assign(**kwargs)


input_df = pd.DataFrame({"product": ["APPLE", "APPLE", "BANANA"],
                         "action": ["  SELL  ", "  BUY  ", "  SELL  "],
                         "quantity": [1, 2, 3],
                         "price (usd)": [3, 5, 8],
                         })

result_df = my_function(input_df)

expected_df = pd.DataFrame({"product": ["APPLE", "APPLE", "BANANA"],
                            "action": ["  SELL  ", "  BUY  ", "  SELL  "],
                            "quantity": [-1, 2, -3],
                            "price (usd)": [0, 5, 0],
                            })

以某种方式,我的lambda表达式中的条件总是返回True,我也认为这可能是一种更简单的方法。有什么想法吗?

2 个答案:

答案 0 :(得分:2)

我们可以做到

s=input_df.action.str.contains('SELL')
input_df.quantity*=s.map({True:-1,False:1})
input_df["price (usd)"]*=~s
input_df
  product    action  quantity  price (usd)
0   APPLE    SELL          -1            0
1   APPLE     BUY           2            5
2  BANANA    SELL          -3            0

功能

def my(x):
...     s=x.action.str.contains('SELL')
...     x.quantity*=s.map({True:-1,False:1})
...     x["price (usd)"]*=~s
...     return (x)


out=my(input_df)
out
  product    action  quantity  price (usd)
0   APPLE    SELL          -1            0
1   APPLE     BUY           2            5
2  BANANA    SELL          -3            0

答案 1 :(得分:1)

以下代码对其进行了修复。我得到了想要的输出。

def my_function(df):
    kwargs = {
        "quantity": df.apply(lambda x: -x["quantity"] if "SELL" in str(x["action"]) else x["quantity"], axis = 1),
        "price (usd)": df.apply(lambda x: 0 if "SELL" in str(x["action"]) else x["price (usd)"], axis = 1),
    }
    return df.assign(**kwargs)