create col("ret_a") =-6ren">
gpt4 book ai didi

python - 替换列表/数据框中的项目的更好方法

转载 作者:行者123 更新时间:2023-12-01 09:02:47 28 4
gpt4 key购买 nike

我必须手动更正一些可能会误导模型的数据。

逻辑是:

    if col("a") not empty & col("b") negative 
=> create col("ret_a") = 2*min(col("a")) - col("a")
else col("ret_a") = col("a")

我正在使用的代码(但不漂亮)是:

# a
df["ret_a"] = np.where(
np.logical_and(pd.notnull(df["a"]), df["b"] < 0),
2*df["a"].min()- df["a"],
df["a"])

问题是我有很多条件......而且我不认为像“<0”这样的条件可以存储在 dict{} 中以使用 pd.map()

然后我尝试使用以下方法构建一些东西:

df['i'] = np.select(conditions, choices,)

带有“i”“条件”和“选择”:

i= [
'RETURN_COM_EQY_', # a
'INTEREST_COVERAGE_RATIO_', # b
'TOT_DEBT_TO_TOT_CAP_', # c
'TOT_DEBT_TO_TOT_EQY_', # d
'NET_DEBT_TO_EBITDA_', # e
'NET_INTEREST_COVERAGE_'] # f

conditions = [
(df['TOTAL_EQUITY_'] < 0), # a
(df['IS_INT_EXPENSE_'] < 0), # b
(df['NET_DEBT_'] < 0), # c
(df['NET_DEBT_'] < 0), # d
(df['NET_DEBT_'] < 0), # e1
(df['EBIT_'] < 0), # e2
(df['IS_INT_EXPENSE_'] < 0)] # f

choices = [
2*df["RETURN_COM_EQY_"].min() - df["RETURN_COM_EQY_"], # a
2*df["INTEREST_COVERAGE_RATIO_"].min() - df["INTEREST_COVERAGE_RATIO_"], # b
2*df["TOT_DEBT_TO_TOT_CAP_"].max() - df["TOT_DEBT_TO_TOT_CAP_"], # c
2*df["TOT_DEBT_TO_TOT_EQY_"].max() - df["TOT_DEBT_TO_TOT_EQY_"], # d
2*df["NET_DEBT_TO_EBITDA_"].max() - df["NET_DEBT_TO_EBITDA_"], # e1
2*df["NET_DEBT_TO_EBITDA_"].min() - df["NET_DEBT_TO_EBITDA_"], # e2
2*df["NET_INTEREST_COVERAGE_"].min() - df["NET_INTEREST_COVERAGE_"], # f
]

该操作的完整代码是:

    # a
df["ret_mean_RETURN_COM_EQY_"] = np.where(
np.logical_and(pd.notnull(df["mean_RETURN_COM_EQY_"]),
df["mean_TOTAL_EQUITY_"] < 0),
2*df["mean_RETURN_COM_EQY_"].min()
- df["mean_RETURN_COM_EQY_"],
df["mean_RETURN_COM_EQY_"])

# b
df["ret_mean_INTEREST_COVERAGE_RATIO_"] = np.where(
np.logical_and(pd.notnull(df["mean_INTEREST_COVERAGE_RATIO_"]),
df["mean_IS_INT_EXPENSE_"] < 0),
2*df["mean_INTEREST_COVERAGE_RATIO_"].min()
- df["mean_INTEREST_COVERAGE_RATIO_"],
df["mean_INTEREST_COVERAGE_RATIO_"])
# c
df["ret_mean_TOT_DEBT_TO_TOT_CAP_"] = np.where(
np.logical_and(pd.notnull(df["mean_TOT_DEBT_TO_TOT_CAP_"]),
df["mean_NET_DEBT_"] < 0),
2*df["mean_TOT_DEBT_TO_TOT_CAP_"].max()
- df["mean_TOT_DEBT_TO_TOT_CAP_"],
df["mean_TOT_DEBT_TO_TOT_CAP_"])

# d
df["ret_mean_TOT_DEBT_TO_TOT_EQY_"] = np.where(
np.logical_and(pd.notnull(df["mean_TOT_DEBT_TO_TOT_EQY_"]),
df["mean_NET_DEBT_"] < 0),
2*df["mean_TOT_DEBT_TO_TOT_EQY_"].max()
- df["mean_TOT_DEBT_TO_TOT_EQY_"],
df["mean_TOT_DEBT_TO_TOT_EQY_"])

# e1
df["ret_mean_NET_DEBT_TO_EBITDA_"] = np.where(
np.logical_and(pd.notnull(df["mean_NET_DEBT_TO_EBITDA_"]),
df["mean_NET_DEBT_"] < 0),
2*df["mean_NET_DEBT_TO_EBITDA_"].max()
- df["mean_NET_DEBT_TO_EBITDA_"],
df["mean_NET_DEBT_TO_EBITDA_"])

# e2
df["ret_mean_NET_DEBT_TO_EBITDA_"] = np.where(
np.logical_and(pd.notnull(df["mean_NET_DEBT_TO_EBITDA_"]),
df["mean_EBIT_"] < 0),
2*df["mean_NET_DEBT_TO_EBITDA_"].min()
- df["mean_NET_DEBT_TO_EBITDA_"],
df["ret_mean_NET_DEBT_TO_EBITDA_"]) # different here

# f
df["ret_mean_NET_INTEREST_COVERAGE_"] = np.where(
np.logical_and(pd.notnull(df["mean_NET_INTEREST_COVERAGE_"]),
df["mean_IS_INT_EXPENSE_"] < 0),
2*df["mean_NET_INTEREST_COVERAGE_"].min()
- df["mean_NET_INTEREST_COVERAGE_"],
df["mean_NET_INTEREST_COVERAGE_"])

最佳答案

这个函数应该完成这个工作:

def func(df, i_n, condition, choice):
name = "ret_" + i_n
df[name] = df[i_n]
df.loc[(pd.notnull(df[i_n])) & (condition), name] = choice

for i_n, condition, choice in zip(i, conditions, choices):
func(df, i_n, condition, choice)

关于python - 替换列表/数据框中的项目的更好方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52335680/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com