1

How to modify the dataframe row values for the condition not met cases i want to modify where column values is between 2 and 8 of all the rows

I tried with iteratetuple for rows and iterateitems for columns and verify the value and update. It is taking some time

is there any direct method for updating each row.

                         2018-07-01  2018-07-02  2018-07-03  2018-07-04

cell_name
1002_NUc_Marathalli_7        0.734       0.550       5.985       0.481
1002_NUc_Marathalli_8        1.338       1.220       0.911       0.601
1002_NUc_Marathalli_9        0.330       1.180       0.754       0.631
1003_IU2_Munnekolalu_7       0.628       0.479       0.988       0.694
1003_IU2_Munnekolalu_8       5.327       6.831       8.387       9.428
vikrant rana
  • 4,509
  • 6
  • 32
  • 72
sriman narayana
  • 359
  • 2
  • 20
  • Can you add your code? What is expected output? – jezrael Dec 20 '18 at 07:47
  • You should possibly check out this answer. A little tweak would help. [https://stackoverflow.com/questions/36909977/update-row-values-where-certain-condition-is-met-in-pandas](https://stackoverflow.com/questions/36909977/update-row-values-where-certain-condition-is-met-in-pandas) – Raghavendra Gupta Dec 20 '18 at 07:54
  • How working my solution? – jezrael Dec 21 '18 at 05:59

1 Answers1

0

Use mask with numpy.random.rand:

np.random.seed(123)

arr = np.random.rand(*df.shape)
print (arr)
[[0.69646919 0.28613933 0.22685145 0.55131477]
 [0.71946897 0.42310646 0.9807642  0.68482974]
 [0.4809319  0.39211752 0.34317802 0.72904971]
 [0.43857224 0.0596779  0.39804426 0.73799541]
 [0.18249173 0.17545176 0.53155137 0.53182759]]

m = (df > 2) & (df < 8)

#replace only values by mask
df1 = df.mask(m, arr)
print (df1)
                        2018-07-01  2018-07-02  2018-07-03  2018-07-04
1002_NUc_Marathalli_7     0.734000    0.550000    0.226851       0.481
1002_NUc_Marathalli_8     1.338000    1.220000    0.911000       0.601
1002_NUc_Marathalli_9     0.330000    1.180000    0.754000       0.631
1003_IU2_Munnekolalu_7    0.628000    0.479000    0.988000       0.694
1003_IU2_Munnekolalu_8    0.182492    0.175452    8.387000       9.428

#replace all rows if exist at least one value True in mask
df2 = df.mask(m.any(axis=1), arr, axis=0)
print (df2)
                        2018-07-01  2018-07-02  2018-07-03  2018-07-04
1002_NUc_Marathalli_7     0.696469    0.286139    0.226851    0.551315
1002_NUc_Marathalli_8     1.338000    1.220000    0.911000    0.601000
1002_NUc_Marathalli_9     0.330000    1.180000    0.754000    0.631000
1003_IU2_Munnekolalu_7    0.628000    0.479000    0.988000    0.694000
1003_IU2_Munnekolalu_8    0.182492    0.175452    0.531551    0.531828

Details:

print (m)
                        2018-07-01  2018-07-02  2018-07-03  2018-07-04
1002_NUc_Marathalli_7        False       False        True       False
1002_NUc_Marathalli_8        False       False       False       False
1002_NUc_Marathalli_9        False       False       False       False
1003_IU2_Munnekolalu_7       False       False       False       False
1003_IU2_Munnekolalu_8        True        True       False       False

print (m.any(axis=1))
1002_NUc_Marathalli_7      True
1002_NUc_Marathalli_8     False
1002_NUc_Marathalli_9     False
1003_IU2_Munnekolalu_7    False
1003_IU2_Munnekolalu_8     True
dtype: bool
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252