![]() |
The widely used relational database management system is known as MysqlDB. The MysqlDB doesn’t understand and accept the value of ‘Nan’, thus there is a need to convert the ‘Nan’ value coming from Pandas or Numpy to ‘None’. In this article, we will see how we can replace Pandas or Numpy ‘Nan’ with a ‘None’. Replacing NaN with None in PandasExample 1: The data frame, for which ‘Nan’ is to be replaced with ‘None’, is as follows:
Python3
Output: Example 2: The data frame, for which ‘Nan’ is to be replaced with ‘None’, is as follows: The provided code uses the Pandas library to replace ‘NaN’ values in a DataFrame df with ‘None’. It does so by using the replace() method with a dictionary mapping where keys (in this case, ‘np.nan’) are replaced by their corresponding values (in this case, ‘None’). The resulting DataFrame ‘replaced_df’ contains ‘None’ in place of ‘NaN’ values. Python3
Output Replacing NaN with None in NumPyExample 1: The data frame, for which ‘Nan’ is to be replaced with ‘None’ , is as follows: [ 1. nan 3. 6. 7.]
It creates a NumPy array named temp, replaces the np.nan values with None using np.where, and then prints the modified temp array. Python3
Output: [1.0 None 3.0 6.0 7.0] Example 2: The data frame, for which ‘Nan’ is to be replaced with ‘None’ , is as follows: [ 4. 5. nan nan 7.] It creates a NumPy array named temp, replaces the np.nan values with None using np.where, and then prints the modified temp array. Python
Output [4.0 5.0 None None 7.0]
|
Reffered: https://www.geeksforgeeks.org
Geeks Premier League |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 15 |