[英]pandas - get minimum value between two columns and assign if two columns are not null
I am trying to figure out how to assign the minimum value between two columns if neither of the columns are not null.如果两列都不是 null,我试图弄清楚如何在两列之间分配最小值。 So given a dataframe with have the following data populated in a row:因此,给定一个 dataframe 并连续填充以下数据:
col1 col2 col3
347 933 338
938 523 211
I'm attempting to assign a temp column to the minimum values between col2 and col3, but the following gives me an error:我正在尝试将临时列分配给 col2 和 col3 之间的最小值,但以下给了我一个错误:
df.loc[df['col2'].notnull() & df['col3'].notnull(), 'TEMP_rank'] = min(df.loc[['col2'], df['col3']]).min().min()
I also have issues with:我也有以下问题:
df.loc[df['col2'].notnull() & df['col3'].notnull(), 'TEMP_rank'] = min(df.loc[['col2'], df['col3']]).min(1)
I'd be looking for the following output (testing between columns 2 & 3):我正在寻找以下 output (在第 2 列和第 3 列之间进行测试):
col1 col2 col3 tempCol
347 933 338 338
938 123 211 123
If you only want to calc min()
when neither are null / NaN this does it.如果您只想在 null / NaN 都不是时计算min()
,则可以这样做。
df = pd.read_csv(io.StringIO("""col1 col2 col3
347 933 338
500 NaN 200
938 523 211"""), sep="\s+")
df = df.assign(
tempCol=lambda dfa: np.where(dfa["col2"].isna()|dfa["col3"].isna(),
np.nan,
dfa.loc[:,["col2","col3"]].min(axis=1))
)
output output
col1 col2 col3 tempCol
0 347 933.0 338 338.0
1 500 NaN 200 NaN
2 938 523.0 211 211.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.