How to handle 'NaN' Values in Dataframe

nan

#1

Hi,

I have requirement that- Import data from one excel to another application

  1. I have imported the data to dataframe.
  2. putting each cell data to application using REST API.
  3. when I dont have any data in excel - the data frame has the value ‘NaN’

while running my code it says -

{“statusCode”:400,“titleMessage”:“Bad Request”,“helpMessage”:“Please correct the input object”,“userMessage”:“There was an error while processing JSON content. The original message was: ‘Non-standard token ‘NaN’: enable JsonParser.Feature.ALLOW_NON_NUMERIC_NUMBERS to allow\n at [Source: org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream@44c37792; line: 1, column: 116]’”}

After that I have used fillna(" ")
it throws error:
{“statusCode”:400,“titleMessage”:“Value not allowed”,“userMessage”:“The numeric attribute ‘’ has an invalid value ({1}).”,“errorCode”:“attDoubleValueInvalid”}

Can Somebody help me out here.

Thanks,
Sachin


#2

Hi Sachin, could you share your code?


#3
df=pd.read_excel("Column Import Template.xlsx")
#df.isnull()
df=df.dropna('')
#df = df.fillna("")
#df1=df.to_json(orient='index')
#df_json = json.loads(df1)

#print df.Name

for index, row in df.iterrows():

# Call REST API POST method to Add  Asset
payload = {
'name':row['Name'],
'domainId':'61be7b2a-73ed-40f7-bd62-7b35b8cd95f8',
'typeId':'00000000-0000-0000-0000-000000031008'
}
headers  = {"Content-Type": "application/json"}
from requests.auth import HTTPBasicAuth
r=requests.post('URL, auth=HTTPBasicAuth('User', 'password'),headers=headers, data=json.dumps(payload))
print(r.text)
           
            #Converting Data to Json Format
resp=r.json()
            #Call The REST API to Add Attributes of Asset 
payloadAttributes =   [
{
"assetId": resp['id'],
"typeId": "00000000-0000-0000-0000-000000003114",
"value":  row['Description']
}
,
{
"assetId": resp['id'],
"typeId": "714853d0-b124-48ff-ae69-6ab5ce3b3037",
"value":  row['Column Name']
}
,
{
"assetId": resp['id'],
"typeId": "00000000-0000-0000-0000-000000000245",
"value": row['Max Length']
}
]            
            #headers  = {"Content-Type": "application/json"}
            #from requests.auth import HTTPBasicAuth
           
        r_postAttributes=requests.post('uRL', auth=HTTPBasicAuth('User', 'PaSS'),headers=headers, data=json.dumps(payloadAttributes))
        print(r_postAttributes.text)

#4

@ pjoshi15 - have added the code.Please check


#5

@sachin123456 Which part of the code is throwing error? If you could share the data as well, then I or anyone from the community could find the issue with much more ease.


#6

@sachin123456 fillna() with “null”

like…
df = df.fillna(“null”)