gpt4 book ai didi

python - pandas 设置一个单元格值等于另一个单元格值

转载 作者:行者123 更新时间:2023-12-01 03:53:59 27 4
gpt4 key购买 nike

我想将 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。 Series 需要与您的 df 保持一致,但事实可能并非如此。因此,要么直接使用 .get_value() 提取值 - 您需要首先获取索引位置 - 或使用 .values,它返回一个 np.array ,并获取该数组的第一个值。

关于python - pandas 设置一个单元格值等于另一个单元格值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37815612/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com