0

When the conditional expression, c1 = 2017-04-01 holds, we want to substitute the number 300.0 for c3.

As a result, 300.0 is substituted for r3 and c3. However, the conditional expression is not r3 columns: c1 = 2017-04-01 is r3 to be organized, so it becomes the line.

import pandas as pd
df=pd.DataFrame(
  [["2017-02-01",10.,100.],
  ["2017-03-01",20.,'Nan'],
  ["2017-04-01",30.,'Nan'],
  ["2017-05-01",40.,'Nan']],
  index=['r1','r2','r3','r4'],
  columns=['c1','c2','c3']
  )
df

###########################
     c1           c2    c3
r1  2017-02-01  10.0    100
r2  2017-03-01  20.0    Nan
r3  2017-04-01  30.0    Nan
r4  2017-05-01  40.0    Nan

What should I do?

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
saru999
  • 7
  • 4

1 Answers1

0

One option is to use

df.loc[(df['c1'] == '2017-04-01'), ['column_you_need_to_change']] = 1.

The above line finds all rows whose c1 column has '2017-04-01' as a value and changes the desired column(s)' values to a number, in the above case 1.

Hope it helps.

nedzad
  • 118
  • 9