我想将pandas dataframe的单元格设置为等于另一个。例如:
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']
设置为数字之外,还有其他选择吗?为什么我不能使用这种方法?
答案 0 :(得分: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
,index
的{{1}}需要与您的pd.Series
对齐,可能不会。因此,要么直接使用df
提取值 - 您需要首先获取索引位置 - 或者使用.get_value()
,它返回.values
,并获取该数组的第一个值。< / p>