How to replace NaN values by Zeroes in a column of a Pandas Dataframe?

后端 未结 13 1414
无人共我
无人共我 2020-11-22 01:37

I have a Pandas Dataframe as below:

      itm Date                  Amount 
67    420 2012-09-30 00:00:00   65211
68    421 2012-09-09 00:00:00   29424
69             


        
13条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-22 01:55

    If you want to fill NaN for a specific column you can use loc:

    d1 = {"Col1" : ['A', 'B', 'C'],
         "fruits": ['Avocado', 'Banana', 'NaN']}
    d1= pd.DataFrame(d1)
    
    output:
    
    Col1    fruits
    0   A   Avocado
    1   B   Banana
    2   C   NaN
    
    
    d1.loc[ d1.Col1=='C', 'fruits' ] =  'Carrot'
    
    
    output:
    
    Col1    fruits
    0   A   Avocado
    1   B   Banana
    2   C   Carrot
    

提交回复
热议问题