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

标签 python pandas numpy

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

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

这是我的中心:

       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/

相关文章:

python - Python 2.7.10 抓取网页时 Unicode 字符替换为问号

python - 使用 map /缩小的反向列表

python - 使用 apply 函数在 pandas 中创建一个具有舍入值的新列

python - 比较两个列表或 numpy 数组的更快方法是什么

python - 如何在 python 中使用 alpha 和 beta 参数绘制 Gamma 分布

python - 在没有管理员权限的情况下安装 rpy2

python - 测试元组是否具有相同的内容

python - 如何识别指定范围的列中的某些行?

python - 将 pandas df 中的所有值乘以组内的最大值

python - 使用 Python 顺序读取和写入核心文件的多线程