今天碰到一個錯誤,一個字典取值報keyError, 一查看key, 字符串類型的數字后面多了小數點0, 變成了float的樣子了。
發現了pandas一個坑:如果列有NAN,則默認給數據轉換為float類型!
來源:https://stackoverflow.com/questions/39666308/pd-read-csv-by-default-treats-integers-like-floats
但是,我們這里不想要讓它轉成float, pandas中有dtype指定列的數據類型,我們可以在read_csv指定我們想要的數據類型。
來源:https://stackoverflow.com/questions/13250046/pandas-csv-import-keep-leading-zeros-in-a-column
dtype_dic= {'subject_id': str,
'subject_number' : 'float'}
df = pd.read_csv(yourdata, dtype = dtype_dic)