gpt4 book ai didi

python - 使用其他 Dataframe 数据作为 iloc 输入从 Dataframe 读取数据

转载 作者:行者123 更新时间:2023-12-01 08:01:02 26 4
gpt4 key购买 nike

我试图使用存储在另一个 df 中的 iloc 坐标从现有 df 中获取值,然后将该值存储在第二个 df 中。

df_source(来源):

        Category1   Category2   Category3
Bucket1 100 200 300
Bucket2 400 500 600
Bucket3 700 800 900

df_coord(坐标):

Index_X Index_Y 
0 0
1 1
2 2

想要:df_坐标

Index_X Index_Y Added
0 0 100
1 1 500
2 2 900

我更熟悉像 SAS 这样的分析语言,一次处理一行数据,所以对我来说自然的方法是:

df_coord['Added'] = df_source.iloc[df_coord[Index_X][df_coord[Index_Y]]

当我尝试此操作时,我收到一个错误,我将其理解为 df_coord[Index_X] 并不引用同一行上的数据。我看过一些帖子,其中使用“axis=1”选项适用于各自的情况,但我不知道如何将其应用于这种情况。谢谢。

最佳答案

您可以索引底层 ndarray,即调用 values 方法,使用 df_coord 中的列作为第一和第二轴:

df_coord['Added'] = df_source.values[df_coord.Index_X, df_coord.Index_Y]

Index_X Index_Y Added
0 0 0 100
1 1 1 500
2 2 2 900

关于python - 使用其他 Dataframe 数据作为 iloc 输入从 Dataframe 读取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55730957/

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