0
import pandas as pd
import numpy as np


df = pd.DataFrame(
    [
        [np.nan, 'None', 3],
        [np.nan, 5, 6],
        [7, 8, 9]
     ], columns=['a', 'b', 'c']
)

df.replace({np.nan: None}, inplace=True)
print(df)

df.replace({'None': None}, inplace=True)
print(df)

      a     b  c
0  None  None  3
1  None     5  6
2     7     8  9
     a    b  c
0  NaN  NaN  3
1  NaN  5.0  6
2  7.0  8.0  9

this is small example fo my case. i wanna replace nan, "None" to None. so i use replace twice first replace method work fine as i thought, but nan was reborn in second replace and all int is changed to float because of nan. i have no idea about why nan is reborn df.replace({'None': None}, inplace=True), how can i fix it?

6
  • What if df.replace({'None': np.nan}, inplace=True)? Commented Mar 3, 2021 at 2:37
  • 2
    you need nullable int dtype: df.replace({'None': None}).astype(pd.Int64Dtype()), docs Commented Mar 3, 2021 at 2:38
  • Could I ask why you want to use None instead of NaN? For most cases, NaN is the best way to indicate a missing value in a DataFrame. Commented Mar 3, 2021 at 2:46
  • @wwnde it doesnt work. Commented Mar 4, 2021 at 14:55
  • @anky i use just "Int64" finally. Commented Mar 4, 2021 at 14:56

3 Answers 3

1

If you want integers in a column with nan values you need to use pd.NA instead. nan is a float and will force an array of integers to become a floating point. Check out the documentation.

Solution

df = pd.DataFrame(
    [
        [np.nan, None, 3],
        [np.nan, 5, 6],
        [7, 8, 9]
    ], 
    columns=['a', 'b', 'c'],
)

# replace np.nan with pd.NA
# then convert columns types to Int32
df.fillna(pd.NA).astype('Int32')
Out[11]:
      a     b  c
0  <NA>  <NA>  3
1  <NA>     5  6
2     7     8  9
Sign up to request clarification or add additional context in comments.

Comments

0

We can do is change it to object

out = df.replace({'None': None}).astype(object)
Out[10]: 
     a    b  c
0  NaN  NaN  3
1  NaN    5  6
2    7    8  9

1 Comment

even i dont use object type, your reply teach me variable way. thanks!
0

It sounds like you want the column type to be an integer instead of a float. You can use the nullable integer dtype introduced in pandas version 0.24.0.

With a column that is a regular integer dtype is automatically converted to a float dtype if it gets a null value. Note that if you use the pandas nullable integer dtype, the column will not become a float and the null value will be represented as <NA> the pandas.NA value.

Read more in the docs.

1 Comment

i know nullable integer type through your reply. thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.