代码之家  ›  专栏  ›  技术社区  ›  Eloise Zeng

创建np。来自pandas dataframe的数组,其中一列包含数组索引的值,另一列包含每个索引的值?

  •  1
  • Eloise Zeng  · 技术社区  · 2 年前

    x y
    0 2. 4.
    1. 3.
    2.

    所有x值都是唯一的。x值还表示numpy数组中相应数字y的索引。

    我有一个np。形状为(6,)的零数组。

    如何有效地修改np。零数组,使其变成 注意,在索引2处,值是4,因为根据数据帧,当x=2时,y=4。

    1 回复  |  直到 2 年前
        1
  •  0
  •   Andrej Kesely    2 年前

    尝试:

    arr = np.zeros(6)
    arr[df["x"]] = df["y"]
    
    print(arr)
    

    打印:

    [0. 0. 4. 1. 0. 9.]