I run the creation of a new column in pandas in a loop (12k values). And it gives this error:

C: \ ProgramData \ Anaconda3 \ lib \ site-packages \ pandas \ core \ indexing.py: 189:

Setting the slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy self._setitem_with_indexer (indexer, value)

Here is the code:

for i in range (a): if T.loc[i]>0: t.loc[i]=T[i]/T1[i] else: t.loc[i]=0 

It gives an error, but generally considers it, but for a very long time ... tell me how to fix it?

  • can you give examples of input and output? - MaxU
  • uploaded to the file sharing, T, T1 source, t - output. dropmefiles.com/0ab1l - Max52 pm
  • judging by the result in column t , the correct formula is T1[i]/T[i] ? - MaxU 5:24 pm

1 answer 1

 In [11]: df['t2'] = np.where(df['T'] > 0, df['T1']/df['T'], 0) 

result:

 In [12]: df Out[12]: T T1 t t2 0 0.0 592.3 0.000000 0.000000 1 0.0 -633.0 0.000000 0.000000 2 0.0 262.8 0.000000 0.000000 3 0.0 -5.4 0.000000 0.000000 4 0.0 1.9 0.000000 0.000000 5 0.0 7.2 0.000000 0.000000 6 0.0 -37.7 0.000000 0.000000 7 0.0 3.3 0.000000 0.000000 8 0.0 6.2 0.000000 0.000000 9 0.0 -3.1 0.000000 0.000000 ... ... ... ... ... 11971 4811.0 1136.0 0.236126 0.236126 11972 5370.0 1540.0 0.286778 0.286778 11973 5875.0 1730.0 0.294468 0.294468 11974 4754.0 1386.0 0.291544 0.291544 11975 5168.0 598.0 0.115712 0.115712 11976 362.3 174.2 0.480817 0.480817 11977 350.3 84.2 0.240365 0.240365 11978 356.8 54.6 0.153027 0.153027 11979 372.6 63.8 0.171229 0.171229 11980 446.8 119.0 0.266338 0.266338 [11981 rows x 4 columns] 

check:

 In [13]: np.allclose(df['t'], df['t2']) Out[13]: True 

measuring processing speed:

 In [14]: %timeit df['t2'] = np.where(df['T'] > 0, df['T1']/df['T'], 0) 1.14 ms ± 68.3 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each) ^^^^^^^