gpt4 book ai didi

python - 使用 numpy 数组或列表中的值更新数据框中的行

转载 作者:太空宇宙 更新时间:2023-11-03 14:59:27 26 4
gpt4 key购买 nike

我有一个数据框,我想根据我在循环中进行的一些计算来迭代更改某些行的值。

例如:如果满足某个条件,那么我想更改中心,即数据帧的一行中的值。

这是我的中心:

       centers=[np.array([ 4.73478261,  3.10869565,  1.44782609, 0.20434783]),
np.array([ 5. , 2.4 , 3.2 , 1.03333333]),
np.array([ 5.135, 3.555, 1.48 , 0.275]),
np.array([ 5.52857143, 4.04285714, 1.47142857, 0.28571429]),
np.array([ 5.596, 2.664, 4.052, 1.252]),
np.array([ 6.01176471, 2.71176471, 4.94705882, 1.79411765]),
np.array([ 6.4 , 2.97058824, 4.55294118, 1.41176471]),
np.array([ 6.49090909, 2.9 , 5.37272727, 1.8 ]),
np.array([ 6.61333333, 3.16 , 5.56666667, 2.28666667]),
np.array([ 7.475, 3.125, 6.3 , 2.05 ])]

然后我将它们转换为数据框

    centersDf = pd.DataFrame(centers)
centersDf

我想做一些类似的事情,

    centersDf[i]=np.array[5,  1,  0  ,  2 ]

这不起作用,但是等效的是什么?因此,我正在重新计算循环中的中心,并且我想更新我的数据框。

最佳答案

centersDf = pd.DataFrame(centers)
centersDf.head()
0 1 2 3
0 4.734783 3.108696 1.447826 0.204348
1 5.000000 2.400000 3.200000 1.033333
2 5.135000 3.555000 1.480000 0.275000
3 5.528571 4.042857 1.471429 0.285714
4 5.596000 2.664000 4.052000 1.252000

centersDf.iloc[0] = np.array([5, 1, 0 , 2 ])
centersDf.head()
0 1 2 3
0 5.000000 1.000000 0.000000 2.000000
1 5.000000 2.400000 3.200000 1.033333
2 5.135000 3.555000 1.480000 0.275000
3 5.528571 4.042857 1.471429 0.285714
4 5.596000 2.664000 4.052000 1.252000

关于python - 使用 numpy 数组或列表中的值更新数据框中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45241992/

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