-1

I am uploading some data frames into snowflake cloud. I had to use the following to transform all field values into string:

data = data.applymap(str)

The only reason I am doing that is that without it, I will get the following error:

TypeError: not all arguments converted during string formatting

The reason for that is there is fields containing numeric values, but not all rows have it, some of them have 'NA' instead. And for data integrity, we cannot replace them with 0s as in our domain, 0 might seems something, and in our work blank is different to the value of 0.

At the beginning, I tried to relace NAa with single quotes '', but then, all fields having numbers were transformed into float. So if a value is 123, it will be 123.0.

How can I replace NA values in a numeric field into completly blank and not '' so the field can still be considered as type INT.

In the image below, I don't want the empty cell to be treated as string, as the other fields will be transformed with the applymap() into floats if they are int:

enter image description here

alim1990
  • 4,656
  • 12
  • 67
  • 130

1 Answers1

1

Detect nan's using np.isnan() and put only non-nan numbers into str().

If you don't want float-typed intgers, just change the mapping fromstr() to str(int()).

Data

Note that column B contains nan which is actually a float number, so its dtype is automatically float.

df = pd.DataFrame({"A": [1 ,2], "B":[3, np.nan]})

print(df)
   A    B
0  1  3.0
1  2  NaN

Code

import numpy as np
df.applymap(lambda el: "" if np.isnan(el) else str(el))

Out[12]: 
   A    B
0  1  3.0
1  2   
Bill Huang
  • 4,491
  • 2
  • 13
  • 31