問題描述
我正在將 csv 文件加載到 Pandas DataFrame 中.對于每一列,如何使用 dtype
參數指定它包含的數據類型?
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 數據(代碼在底部)...
- 但是如何指定時間數據...
- 和分類數據,例如因子或布爾值?我試過
np.bool_
和pd.tslib.Timestamp
沒有運氣.
- 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 有很多選項可以處理你提到的所有情況.您可能想嘗試 dtype={'A': datetime.datetime},但通常您不需要 dtypes,因為 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.
對于日期,則需要指定 parse_date 選項:
parse_dates : boolean, list of ints or names, list of lists, or dict
keep_date_col : boolean, default False
date_parser : function
一般來說,要轉換布爾值,您需要指定:
true_values : list Values to consider as True
false_values : list Values to consider as False
這會將列表中的任何值轉換為布爾值 true/false.對于更一般的轉換,您很可能需要
Which will transform any value in the list to the boolean true/false. For more general conversions you will most likely need
轉換器:字典.用于轉換某些列中的值的可選函數字典.鍵可以是整數或列標簽
converters : dict. optional Dict of functions for converting values in certain columns. Keys can either be integers or column labels
雖然密集,但請在此處查看完整列表: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
這篇關于為 pandas.read_csv 指定正確的 dtypes 以獲取日期時間和布爾值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!