將numpy中的nan替換為python中的None


問題說明

想將numpy數據批量插入mysql,發現如下報錯:
pymysql.err.ProgrammingError: nan can not be used with MySQL

替換方法

## data是numpy數據,格式入下:
data = [[nan, nan, '李幼斌', 'star'],
       [nan, nan, '孟非', 'star']], dtype=object)

import pandas as pd
data[pd.isna(data)] = None  # 將nan替換為None
data= data.tolist()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM