0

I have a dataframe with date and value fields. I need to change the value in a field if the date matches. Here is a dataset:

        date        confirmed   deaths  

701     2020-03-02  NaN         NaN 
702     2020-03-03  NaN         NaN 
703     2020-03-04  NaN         NaN 
704     2020-03-05  NaN         NaN 
705     2020-03-06  NaN         NaN 

Specially made a slice of the data to make it clearer. I want to enter the value 2 for the date 2020-03-04 in the deaths field. How can I do this? Based exactly on the date field.

At the output I want to get this:

        date        confirmed   deaths  

701     2020-03-02  NaN         NaN 
702     2020-03-03  NaN         NaN 
703     2020-03-04  NaN         2
704     2020-03-05  NaN         NaN 
705     2020-03-06  NaN         NaN 

I tried several ways but it didn't work for some reason. Can you help?

kostya ivanov
  • 613
  • 5
  • 15
  • `df.loc[df['date'].eq('2020-03-04'), 'deaths'] = 2` – mozway Feb 09 '22 at 14:43
  • @mozway, Hello again. I'm trying to add the data of the first infected to a dataset based on news data. I get with error ```int' object has no attribute 'loc'```( In the dataset itself, in this record field, there is a float, but even with ``` = 2.0 ``` i get ```'float' object has no attribute 'loc'``` – kostya ivanov Feb 09 '22 at 14:48
  • Looks like you assigned a float to `df` ;) – mozway Feb 09 '22 at 14:55
  • @mozway, And how can I solve it? – kostya ivanov Feb 09 '22 at 14:59
  • don't do the mistake on the first place ;) Please provide fully reproducible code, it just looks like you are having typos or basic errors here. – mozway Feb 09 '22 at 15:00

0 Answers0