【发布时间】:2022-01-01 12:45:50
【问题描述】:
我需要将列 R2ESL 或 MCL 中的数值(以较小者为准)附加到每一行(例如 A、B、C)的新列“较小值”中。
例如,对于参数 A,值为 7 的 MCL 小于值为 10 的 R2ESL。我想将 7 放入一个新列(较小的值),但在同一行 (A)。
到目前为止,我只能在 for 循环中使用“附加”来附加限制条件列。
| param | R2ESL | MCL | Limiting Criteria | Lesser Value |
|---|---|---|---|---|
| A | 10 | 7 | MCL | 7 |
| B | 100 | 150 | R2ESL | 100 |
| C | 55 | 55 | equal | 55 |
excel_df = pd.read_excel('ESLs_MCL_Comparison_copy.xls')
# Changing data type in columns to float
R2ESL_float = [float(item) for item in R2ESL]
print(R2ESL_float)
MCL_float = [float(item) for item in MCL]
print(MCL_float)
# Comparing the values in each column
result = []
result2 = []
for (R2ESL_float, MCL_float) in zip(R2ESL_float, MCL_float):
if R2ESL_float > MCL_float:
result.append("R2ESL")
# result2.append(param_row_value_R2ESL) <-- Need help here
elif R2ESL_float < MCL_float:
result.append("MCL")
# result2.append(param_row_value_MCL) <-- Need help here
elif R2ESL_float == MCL_float:
result.append("Equal")
# result2.append("param_row_value_MCL_or_R2ESL") <-- Need help here
else:
result.append("null")
# result2.append("null")
excel_df["Limiting Criteria"] = result
# excel_df["Lesser Value"] = result2
print(excel_df)
我是python新手,才学几周,请解释清楚。
【问题讨论】:
标签: python pandas for-loop append