我想在python中計算行之間的差異。我知道我可以用diff()來做這個。但我想
嘗試使用 "for回圈"。我嘗試了下面的代碼,但我得到的錯誤是 "KeyError: 2 "行
if pdf.loc[i 1,'A'] != pdf.loc[i,'A']:
如果我想做下面的計算,我應該怎么做,請幫助。
print(loc[2,'A'] * loc[1,'A'] 3)
print(loc[3,'A'] * loc[2,'A'] 3)
print(loc[4,'A'] * loc[3,'A'] 3)
...
print(loc[i, 'A'] * loc[i-1, 'A'] 3)
顯示錯誤的代碼是 "KeyError: 2"
for i, dura in pdf.iterrows() 。
if i < pdf.shape[0] 。
if pdf.loc[i 1,'A'] != pdf.loc[i,'A'] 。
print("a")
else:
print("b")
else:
print(" finished")
uj5u.com熱心網友回復:
iterrows()是遍歷pandas資料框架的最慢的方法之一。使用itertuples()代替(還有其他更快的選項)
uj5u.com熱心網友回復:
使用iloc你可以,enumerate(df,2)從2開始,正如你在問題中寫的那樣。
df = pd.DataFrame({
'A': [ 1, 2, 3, 4, 5]
})
for i, dura in enumerate(df,2)。
print(df.iloc[i]['A'/span>]*df. iloc[i-1]['A'] 3)
輸出:
9 # 3 * 2 3
15 # 4 * 3 3
23 # 5 * 4 3
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/307644.html
標籤:
下一篇:總結出2個具有納米值的資料幀
