1

I have a dataframe, and I want to iterate change the values of some rows depending on some calculations I'm doing in a loop.

So for example: if a condition is met, Then I want to change the centers, that are the values in a row of my dataframe.

This are my centers:

       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 ])]

I then convert them to a dataframe

    centersDf = pd.DataFrame(centers)
    centersDf

and I would like to do something like,

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

This doesn't work, but what could be the equivalent? So, I'm recalculating the centers in my loop, and I want to update my dataframe.

1
  • Assign to centersDf.iloc[i] instead of centersDf[i]. The latter refers to the i'th column. Commented Jul 21, 2017 at 16:03

2 Answers 2

1
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
Sign up to request clarification or add additional context in comments.

Comments

0

When you pass a scalar value to the __getitem__ method (that is using the []) pandas mathches up against column names. So centersDf[0] is the 0th column. You get an error because your trying to assign an array of length 4 to a column of length 10 and that makes no sense.

If you want to be able to assign by column name, create your dataframe as it's transpose

centersDf = pd.DataFrame(centers).T

Then

centersDf[0] = [5,  1,  0 ,  2]

Works fine

centersDf

   0         1      2         3      4         5         6         7         8      9
0  5  5.000000  5.135  5.528571  5.596  6.011765  6.400000  6.490909  6.613333  7.475
1  1  2.400000  3.555  4.042857  2.664  2.711765  2.970588  2.900000  3.160000  3.125
2  0  3.200000  1.480  1.471429  4.052  4.947059  4.552941  5.372727  5.566667  6.300
3  2  1.033333  0.275  0.285714  1.252  1.794118  1.411765  1.800000  2.286667  2.050

Otherwise, just use loc as has been suggested already.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.