I am new to Python and would like to assign a value based on mathematical operations, eg “right” if >, “left” if <, “equal” if ==, within a list comprehension.
I have tried the below, but it throws an error. Can multiple conditions be specified in a single list comprehension in this way, where each “elif” generates a different output, or will I need to use a loop?
# problem 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],
}
again I am still fairly new to programming; apologies if I do not immediately understand the solution. any guidance is appreciated!