2016-06-14 94 views
1

我想設置一個熊貓數據框的單元格等於另一個。例如:熊貓設置一個單元格值等於另一個

station_dim.loc[station_dim.nlc==573,'longitude']=station_dim.loc[station_dim.nlc==5152,'longitude'] 

然而,當我檢查

station_dim.loc[station_dim.nlc==573,'longitude'] 

它返回NaN

除了直接設置station_dim.loc[station_dim.nlc==573,'longitude']了一些,我有什麼別的選擇嗎?爲什麼我不能使用這種方法?

+1

什麼'station_dim.loc [station_dim.nlc == 5152,'經度']'本身返回? – IanS

+0

我可以手動分配給station_dim.loc的浮點數[station_dim.nlc == 573,'經度']。但是,如果我需要擴大規模,我無法手動完成 –

回答

2

看看get_value,或使用.values

station_dim.loc[station_dim.nlc==573,'longitude']=station_dim.loc[station_dim.nlc==5152,'longitude'].values[0] 

對於分配工作 - .loc[]將返回一個pd.Series,即pd.Seriesindex需要與你的df對齊,它有可能沒有按「T。因此,要麼使用.get_value()直接提取值 - 您需要首先獲取索引位置 - 或使用.values,它將返回np.array,並獲取該數組的第一個值。

+0

解釋該分配是基於索引完成的,該索引很可能解釋了「NaN」值。 – IanS

相關問題