Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Machine Learning by (19k points)

I have a 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 421 2012-09-16 00:00:00 29877 

70 421 2012-09-23 00:00:00 30990 

71 421 2012-09-30 00:00:00 61303 

72 485 2012-09-09 00:00:00 71781 

73 485 2012-09-16 00:00:00 NaN 

74 485 2012-09-23 00:00:00 11072 

75 485 2012-09-30 00:00:00 113702 

76 489 2012-09-09 00:00:00 64731 

77 489 2012-09-16 00:00:00 NaN

when I try to .apply a function to the Amount column I get the following error.

ValueError: cannot convert float NaN to integer

I have tried applying a function using .isnan from the Math Module I have tried the pandas .replace attribute I tried the .sparse data attribute from pandas 0.9 I have also tried if NaN == NaN statement in a function. I have also looked at this article How do I replace NA values with zeros in an R dataframe? whilst looking at some other articles. All the methods I have tried have not worked or do not recognize NaN. Any Hints or solutions would be appreciated.

1 Answer

0 votes
by (33.1k points)

You can use DataFrame.fillna() method for this problem. This function fills the null value with the given value in the dataframe. 

For example:

In [12]: df[1].fillna(0, inplace=True) 

Out[12]:

0    0.000000 

1    0.570994 

2    0.000000 

3   -0.229738 

4    0.000000 

Name: 1 

In [13]: df 

Out[13]: 

     0               1 

0   NaN           0.000000 

1  -0.494375   0.570994 

2   NaN           0.000000 

3   1.876360  -0.229738 

4   NaN           0.000000

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...