我想将 pandas 数据帧的一个单元格设置为等于另一个单元格。例如:
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']
设置为数字之外,我还有什么选择?为什么我不能使用这个方法?
最佳答案
看看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 的
需要与您的 index
。 Seriesdf
保持一致,但事实可能并非如此。因此,要么直接使用 .get_value()
提取值 - 您需要首先获取索引位置 - 或使用 .values
,它返回一个 np.array
,并获取该数组的第一个值。
关于python - pandas 设置一个单元格值等于另一个单元格值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37815612/