[英]How to save csv file in python with datatype of one particular columns has to be int32?
在將列轉換為數據類型np.int32
后,我正在保存 dataframe df
。 但是,當我重新加載它時,我發現數據類型是np.int64
。 我怎樣才能解決這個問題?
# creating a dictionary
dict = {'Id':[1, 2, 3, 4],
'Name':['John', 'Rahul', 'Jaspreet', 'Watson']}
##.....................................................
df = pd.DataFrame(dict)
print(df)
# output::
# Id Name
# 0 1 John
# 1 2 Rahul
# 2 3 Jaspreet
# 3 4 Watson
df['Id']=df['Id'].values.astype(np.int32)
df['Id'].dtype
# output : dtype('int32')
df.to_csv('Justtry.csv',index=False)
df1 = pd.read_csv('Justtry.csv')
df1['Id'].dtype
# output: dtype('int64')
當你讀取csv時,你可以將參數設置為int32
data = pd.read_csv(..., dtype={'Id': np.int32})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.