[英]How to iterate through and compare values of data frames?
我想通過迭代和在兩個數據幀,該值進行比較PORResult
, p90Result
,然后更新的第三數據幀中的值, CountResult
基於該比較。 如果值PORResult
比的值大p90result
我想更新的價值CountResult
是= 1。所有的數據幀的大小相同(121x365),並具有相同的列名和索引。 這是我被困的地方:
for index, row1 in PORResult.iterrows():
for index, row2 in p90Result.iterrows():
if PORResult.loc[index,row1]>p90Result.loc[index,row2]:
CountResult.at[index,row1]=1
當我嘗試此操作時,我收到以下錯誤消息:“[Float64Index([ nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,\\n ...\\n nan, nan, nan , nan, nan, nan, nan, nan, nan, 46.0],\\n dtype='float64', name='day', length=365)] 在 [columns]" 我不知道去哪里從這里。 這是否意味着我的數據框設置不正確,或者我沒有正確執行循環? 我是 python 的新手,所以感謝任何幫助!
此循環將第一個數據幀的所有行與第二個數據幀的所有行進行比較。 您應該進行循環,例如for i in range(PORResult.shape[0]): for j in range (PORResult.shape[1]):
這就是我解決問題的方法
for day in PORResult.columns:
for year in PORResult.index:
tmax = PORResult.loc[year, day]
perc = p90Result.loc[year, day].values[0]
if tmax > perc:
CountResult.loc[year, day] = 1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.