我正在嘗試存盤兩個 DataFrame 具有相同值的索引。例如...
current_gages = 95x4 DataFrame 帶有以下標題:site_no、sitename、Lat、Lon
ucol_gages = 253 x 2 具有以下標題的資料框:site_no, area
我想找到這兩個資料幀的索引在哪里具有相同的站點編號 (site_no)。
我正在處理以下問題:
count1=0
gages_idx=[]
for j in range(len(current_gages)):
if ucol_gages['site_no'][count1] == current_gages['site_no'][j]:
gages_idx.append([count1, j])
count1 =1
elif ucol_gages['site_no'][count1] != current_gages['site_no'][j]:
count1 =0
回圈在第一個 if 陳述句的 [4,4] 處停止,然后為了確保當前量具中沒有任何其他行,我添加了 elif 陳述句以遍歷剩余的行。
我知道 ucol_gages 和 current_gages 匹配的下一個索引是:ucol_gages['site_no'][5] 和 current_gages['site_no'][4]。
有沒有更簡單的方法來做到這一點?我需要確保遍歷 DF 中行索引的所有變體以確定任何可能的匹配項。
uj5u.com熱心網友回復:
要獲得 ucol_gages 資料幀的匹配索引,您可以執行以下操作
ucol_gages[ucol_gages['site_no'].isin(pd.merge(ucol_gages, current_gages, on=['site_no'], how='inner')['site_no'])].index.values.tolist()
要獲取 current_gages 資料幀的匹配索引,您可以執行以下操作
current_gages[current_gages['site_no'].isin(pd.merge(current_gages, ucol_gages, on=['site_no'], how='inner')['site_no'])].index.values.tolist()
希望這可以幫助 ;)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/335819.html
上一篇:如何調整Python的sys.argv以相應地更改其長度
下一篇:如何在sas中使用陣列?
