pandas.read_csv 中的 dtype 和转换器有什么区别?
What's the difference between dtype and converters in pandas.read_csv?
pandas 函数 read_csv() 读取 .csv 文件。它的文档是 here
根据文档,我们知道:
dtype : Type name or dict of column -> type, default None Data type
for data or columns. E.g. {‘a’: np.float64, ‘b’: np.int32}
(Unsupported with engine=’python’)
和
converters : dict, default None Dict of functions for converting
values in certain columns. Keys can either be integers or column
labels
使用这个函数时,我可以调用
pandas.read_csv('file',dtype=object)
或 pandas.read_csv('file',converters=object)
。显然,converter,它的名字可以说数据类型将被转换,但我想知道 dtype 的大小写?
语义差异在于 dtype
允许您指定如何处理值,例如,作为数字或字符串类型。
Converters 允许您解析输入数据以使用转换函数将其转换为所需的数据类型,例如,将字符串值解析为日期时间或其他所需的数据类型。
这里我们看到 pandas 试图嗅探类型:
In [2]:
df = pd.read_csv(io.StringIO(t))
t="""int,float,date,str
001,3.31,2015/01/01,005"""
df = pd.read_csv(io.StringIO(t))
df.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 1 entries, 0 to 0
Data columns (total 4 columns):
int 1 non-null int64
float 1 non-null float64
date 1 non-null object
str 1 non-null int64
dtypes: float64(1), int64(2), object(1)
memory usage: 40.0+ bytes
您可以从上面看到 001
和 005
被视为 int64
但日期字符串保持为 str
.
如果我们说一切都是 object
那么基本上一切都是 str
:
In [3]:
df = pd.read_csv(io.StringIO(t), dtype=object).info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 1 entries, 0 to 0
Data columns (total 4 columns):
int 1 non-null object
float 1 non-null object
date 1 non-null object
str 1 non-null object
dtypes: object(4)
memory usage: 40.0+ bytes
这里我们强制int
列为str
并告诉parse_dates
使用date_parser解析日期列:
In [6]:
pd.read_csv(io.StringIO(t), dtype={'int':'object'}, parse_dates=['date']).info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 1 entries, 0 to 0
Data columns (total 4 columns):
int 1 non-null object
float 1 non-null float64
date 1 non-null datetime64[ns]
str 1 non-null int64
dtypes: datetime64[ns](1), float64(1), int64(1), object(1)
memory usage: 40.0+ bytes
同样,我们可以通过 to_datetime
函数来转换日期:
In [5]:
pd.read_csv(io.StringIO(t), converters={'date':pd.to_datetime}).info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 1 entries, 0 to 0
Data columns (total 4 columns):
int 1 non-null int64
float 1 non-null float64
date 1 non-null datetime64[ns]
str 1 non-null int64
dtypes: datetime64[ns](1), float64(1), int64(2)
memory usage: 40.0 bytes
我会说 converters
的主要目的是操纵列的值,而不是数据类型。 @EdChum 分享的答案侧重于 dtypes
的想法。它使用 pd.to_datetime
函数。
在这篇文章 https://medium.com/analytics-vidhya/make-the-most-out-of-your-pandas-read-csv-1531c71893b5 中关于 转换器 的区域中,您将看到一个更改 csv 列的示例,其值例如“185 lbs.”,变成从文本列中删除“lbs”的东西。这更多是 read_csv
converters
参数背后的想法。
.csv 是什么样子(如果图像没有显示,请转到文章。)
#creating functions to clean the columns
w = lambda x: (x.replace('lbs.',''))
r = lambda x: (x.replace('"',''))
#using converters to apply the functions to the columns
fighter = pd.read_csv('raw_fighter_details.csv' ,
converters={'Weight':w , 'Reach':r },
header=0,
usecols = [0,1,2,3])
fighter.head(15)
Weight栏使用converters
后的DataFrame
pandas 函数 read_csv() 读取 .csv 文件。它的文档是 here
根据文档,我们知道:
dtype : Type name or dict of column -> type, default None Data type for data or columns. E.g. {‘a’: np.float64, ‘b’: np.int32} (Unsupported with engine=’python’)
和
converters : dict, default None Dict of functions for converting values in certain columns. Keys can either be integers or column labels
使用这个函数时,我可以调用
pandas.read_csv('file',dtype=object)
或 pandas.read_csv('file',converters=object)
。显然,converter,它的名字可以说数据类型将被转换,但我想知道 dtype 的大小写?
语义差异在于 dtype
允许您指定如何处理值,例如,作为数字或字符串类型。
Converters 允许您解析输入数据以使用转换函数将其转换为所需的数据类型,例如,将字符串值解析为日期时间或其他所需的数据类型。
这里我们看到 pandas 试图嗅探类型:
In [2]:
df = pd.read_csv(io.StringIO(t))
t="""int,float,date,str
001,3.31,2015/01/01,005"""
df = pd.read_csv(io.StringIO(t))
df.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 1 entries, 0 to 0
Data columns (total 4 columns):
int 1 non-null int64
float 1 non-null float64
date 1 non-null object
str 1 non-null int64
dtypes: float64(1), int64(2), object(1)
memory usage: 40.0+ bytes
您可以从上面看到 001
和 005
被视为 int64
但日期字符串保持为 str
.
如果我们说一切都是 object
那么基本上一切都是 str
:
In [3]:
df = pd.read_csv(io.StringIO(t), dtype=object).info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 1 entries, 0 to 0
Data columns (total 4 columns):
int 1 non-null object
float 1 non-null object
date 1 non-null object
str 1 non-null object
dtypes: object(4)
memory usage: 40.0+ bytes
这里我们强制int
列为str
并告诉parse_dates
使用date_parser解析日期列:
In [6]:
pd.read_csv(io.StringIO(t), dtype={'int':'object'}, parse_dates=['date']).info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 1 entries, 0 to 0
Data columns (total 4 columns):
int 1 non-null object
float 1 non-null float64
date 1 non-null datetime64[ns]
str 1 non-null int64
dtypes: datetime64[ns](1), float64(1), int64(1), object(1)
memory usage: 40.0+ bytes
同样,我们可以通过 to_datetime
函数来转换日期:
In [5]:
pd.read_csv(io.StringIO(t), converters={'date':pd.to_datetime}).info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 1 entries, 0 to 0
Data columns (total 4 columns):
int 1 non-null int64
float 1 non-null float64
date 1 non-null datetime64[ns]
str 1 non-null int64
dtypes: datetime64[ns](1), float64(1), int64(2)
memory usage: 40.0 bytes
我会说 converters
的主要目的是操纵列的值,而不是数据类型。 @EdChum 分享的答案侧重于 dtypes
的想法。它使用 pd.to_datetime
函数。
在这篇文章 https://medium.com/analytics-vidhya/make-the-most-out-of-your-pandas-read-csv-1531c71893b5 中关于 转换器 的区域中,您将看到一个更改 csv 列的示例,其值例如“185 lbs.”,变成从文本列中删除“lbs”的东西。这更多是 read_csv
converters
参数背后的想法。
.csv 是什么样子(如果图像没有显示,请转到文章。)
#creating functions to clean the columns
w = lambda x: (x.replace('lbs.',''))
r = lambda x: (x.replace('"',''))
#using converters to apply the functions to the columns
fighter = pd.read_csv('raw_fighter_details.csv' ,
converters={'Weight':w , 'Reach':r },
header=0,
usecols = [0,1,2,3])
fighter.head(15)
Weight栏使用converters
后的DataFrame