Pandas NaN to None

October 22, 2019


Python Pandas 讀取 csv 檔把 NaN 轉成 None

import pandas as pd
import numpy as np

df = pd.read_csv("xxx.csv", encoding='utf8', header=None, low_memory=False)
df = df.replace({ None})

2021/05/25 update


FutureWarning: The module is deprecated and will be removed from pandas in a future version. Import numpy directly instead


df = df.replace({np.nan: None})

Profile picture

Written by dsewnr who lives and works in Taiwan has a great enthusiasm for software engineering. Please feel free to contact me by email.