我有一堆 elif 函式:
if 0 <= A.iloc[Row] <= 5:
B = C.iloc[0,0]
elif 5 <= A.iloc[Row] <= 10:
B = C.iloc[1,0]
elif 10 <= A.iloc[Row] <= 15:
B = C.iloc[2,0]
elif 15 <= A.iloc[Row] <= 20:
B = C.iloc[3,0]
...
有沒有辦法簡化我的 elif 函式,以便它們迭代高達 95<x<100?
我考慮過使用while回圈,但我似乎無法弄清楚。(我只處于學習 Python 的初級階段)。
uj5u.com熱心網友回復:
您可以使用 for 回圈
for i in range(0, 100, 5):
if i <= A.iloc[Row] < i 5:
B = C.iloc[i // 5, 0]
或數學
chunk = A.iloc[Row] // 5
B = C.iloc[chunk, 0]
請注意,<= A.iloc[Row] <=關于邏輯不是很正確,應該是<= A.iloc[Row] <因為邊界不應該出現在 2 種情況下
uj5u.com熱心網友回復:
使用 while 回圈,它看起來像這樣:
a = 0
while a <= 100:
if a <= A.iloc[Row] <= a 5:
B = C.iloc[0,0]
a = 5
uj5u.com熱心網友回復:
由于重疊,問題中的邏輯似乎存在缺陷。
如果我們假設 B = C.iloc[0,0] 當 0 <= A.iloc[Row] < 5 時,那么您只需要:
B = C.iloc[A.iloc[Row]//5, 0]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/487573.html
上一篇:Excel上的嵌套IF陳述句
下一篇:垂直組合查詢結果
