Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing nan with blanks in Python

Below is my dataframe:

Id,ReturnCreated,ReturnTime,TS_startTime
O108808972773560,Return Not Created,nan,2018-08-23 12:30:41
O100497888936380,Return Not Created,nan,2018-08-18 14:57:20
O109648374050370,Return Not Created,nan,2018-08-16 13:50:06
O112787613729150,Return Not Created,nan,2018-08-16 13:15:26
O110938305325240,Return Not Created,nan,2018-08-22 11:03:37
O110829757146060,Return Not Created,nan,2018-08-21 16:10:37

I want to replace the nan with Blanks. Tried the below code, but its not working.

import pandas as pd
import numpy as np
df = pd.concat({k:pd.Series(v) for k, v in ordercreated.items()}).unstack().astype(str).sort_index()
df.columns = 'ReturnCreated  ReturnTime  TS_startTime'.split()
df1 = df.replace(np.nan,"", regex=True)
df1.to_csv('OrderCreationdetails.csv')

Kindly help me understand where i am going wrong and how can i fix the same.

like image 367
Soubhik Banerjee Avatar asked Sep 25 '18 08:09

Soubhik Banerjee


People also ask

How do I remove NaN values in Python?

To remove NaN from a list using Python, the easiest way is to use the isnan() function from the Python math module and list comprehension. You can also use the Python filter() function. The Python numpy module also provides an isnan() function that we can use to check if a value is NaN.


Video Answer


3 Answers

You should try DataFrame.fillna() method

https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.fillna.html

In your case:

df1 = df.fillna("")

should work I think

like image 161
Louis D. Avatar answered Oct 16 '22 12:10

Louis D.


I think nans are strings, because .astype(str). So need:

df1 = df.replace('nan',"")
like image 5
jezrael Avatar answered Oct 16 '22 12:10

jezrael


Either you can use df.fillna("") (i think that will perform better) or simple replace that values with blank

df1 = df.replace('NaN',"")
like image 1
Umer Avatar answered Oct 16 '22 12:10

Umer