iT邦幫忙

0

pandas to_csv

新手發問
寫了一個程式去很多個csv檔找特徵,需要把這些找到的存成一個csv檔
但type好像跑掉了
原本的
https://ithelp.ithome.com.tw/upload/images/20210310/20128008006tpI7wjv.png
後來的
https://ithelp.ithome.com.tw/upload/images/20210310/20128008BmrbASB50I.png

附上程式碼
https://ithelp.ithome.com.tw/upload/images/20210310/20128008dvIXiECccd.png
求大神幫忙

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

2
froce
iT邦大師 1 級 ‧ 2021-03-10 10:11:57

你可以在read_csv的時候加參數(dtype)去聲明類型。

然後我真的覺得我的大師頭銜是搬官方文件搬來的。給初學者一些建議,寫程式先去查一下官方文件,不要埋著頭苦幹,然後還幹不出來。

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html

dtypeType name or dict of column -> type, optional

Data type for data or columns. E.g. {‘a’: np.float64, ‘b’: np.int32, ‘c’: ‘Int64’} Use str or object together with suitable na_values settings to preserve and not interpret dtype. If converters are specified, they will be applied INSTEAD of dtype conversion.
pd.read_csv(file.csv, dtype={"row_id": np.int32, ....})
0
I code so I am
iT邦高手 1 級 ‧ 2021-03-11 09:25:36

csv 檔只是單純的文字檔,不含資料型別的資訊,read_csv 會根據資料自動判別資料型別,建議以記事本打開檢查是否有null value或特別的符號。

我要發表回答

立即登入回答