pandas将一个单元格值设置为等于另一个单元格

时间:2016-06-14 14:59:19

标签: python pandas select

我想将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']设置为数字之外,还有其他选择吗?为什么我不能使用这种方法?

1 个答案:

答案 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.Seriesindex的{​​{1}}需要与您的pd.Series对齐,可能不会。因此,要么直接使用df提取值 - 您需要首先获取索引位置 - 或者使用.get_value(),它返回.values,并获取该数组的第一个值。< / p>