問(wèn)題描述
我正在將 csv 文件加載到 Pandas DataFrame 中.對(duì)于每一列,如何使用 dtype
參數(shù)指定它包含的數(shù)據(jù)類型?
I am loading a csv file into a Pandas DataFrame. For each column, how do I specify what type of data it contains using the dtype
argument?
- 我可以使用 numeric 數(shù)據(jù)(代碼在底部)...
- 但是如何指定時(shí)間數(shù)據(jù)...
- 和分類數(shù)據(jù),例如因子或布爾值?我試過(guò)
np.bool_
和pd.tslib.Timestamp
沒(méi)有運(yùn)氣.
- I can do it with numeric data (code at bottom)...
- But how do I specify time data...
- and categorical data such as factors or booleans? I have tried
np.bool_
andpd.tslib.Timestamp
without luck.
代碼:
import pandas as pd
import numpy as np
df = pd.read_csv(<file-name>, dtype={'A': np.int64, 'B': np.float64})
推薦答案
read_csv 有很多選項(xiàng)可以處理你提到的所有情況.您可能想嘗試 dtype={'A': datetime.datetime},但通常您不需要 dtypes,因?yàn)?pandas 可以推斷類型.
There are a lot of options for read_csv which will handle all the cases you mentioned. You might want to try dtype={'A': datetime.datetime}, but often you won't need dtypes as pandas can infer the types.
對(duì)于日期,則需要指定 parse_date 選項(xiàng):
parse_dates : boolean, list of ints or names, list of lists, or dict
keep_date_col : boolean, default False
date_parser : function
一般來(lái)說(shuō),要轉(zhuǎn)換布爾值,您需要指定:
true_values : list Values to consider as True
false_values : list Values to consider as False
這會(huì)將列表中的任何值轉(zhuǎn)換為布爾值 true/false.對(duì)于更一般的轉(zhuǎn)換,您很可能需要
Which will transform any value in the list to the boolean true/false. For more general conversions you will most likely need
轉(zhuǎn)換器:字典.用于轉(zhuǎn)換某些列中的值的可選函數(shù)字典.鍵可以是整數(shù)或列標(biāo)簽
converters : dict. optional Dict of functions for converting values in certain columns. Keys can either be integers or column labels
雖然密集,但請(qǐng)?jiān)诖颂幉榭赐暾斜?http://pandas.pydata.org/pandas-docs/stable/generated/pandas.io.parsers.read_csv.html
Though dense, check here for the full list: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.io.parsers.read_csv.html
這篇關(guān)于為 pandas.read_csv 指定正確的 dtypes 以獲取日期時(shí)間和布爾值的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!