我是Python新手,想根据数学运算分配一个值,例如“右”如果>,“左”如果<, "equal" if ==, within a list comprehension.
我已经尝试了以下方法,但它引发了错误。可以通过这种方式在单个列表理解中指定多个条件,其中每个“elif”生成不同的输出,还是我需要使用循环?
完全可重现的示例:
from sklearn.datasets import load_iris
bunch = load_iris(as_frame=True)
df = bunch.data.reset_index().rename(columns={"index": "id"}).merge(bunch.target.reset_index().rename(columns={"index": "id"})).drop(["id"], axis=1)
# question is in last row, "skew"
datasummary_dct = {
"50%": [df[col].median().round(2) if any(t in str(df[col].dtype) for t in ("float", "int", "time")) else " " for col in df.columns],
"mean": [df[col].mean().round(2) if any(t in str(df[col].dtype) for t in ("float", "int", "time")) else " " for col in df.columns],
"skew": ["left" if df[col].median() > df[col].mean() else "right" if df[col].median() < df[col].mean() else "equal" if df[col].median()==df[col].mean() if any(t in str(df[col].dtype) for t in ("float", "int", "time")) else " " for col in df.columns],
}
再说一遍,我对编程还很陌生;如果我不能立即理解解决方案,我深表歉意。任何指导表示赞赏!
您可以使用
if
,而不是复杂的嵌套 np.select
,这样更具可读性:
datasummary_dct = {
"skew": [
np.select(
[df[col].median() > df[col].mean(), df[col].median() < df[col].mean()],
["right", "left"],
"equal",
)
if any(t in str(df[col].dtype) for t in ("float", "int", "time"))
else " "
for col in df.columns
],
}
print(pd.DataFrame(datasummary_dct))
输出:
skew
0 left
1 left
2 right
3 right
4 equal