Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 什么';在pandas.read\u csv中,数据类型和转换器之间的区别是什么?_Python_Pandas_Types_Converter_Type Inference - Fatal编程技术网

Python 什么';在pandas.read\u csv中,数据类型和转换器之间的区别是什么?

Python 什么';在pandas.read\u csv中,数据类型和转换器之间的区别是什么?,python,pandas,types,converter,type-inference,Python,Pandas,Types,Converter,Type Inference,函数read_csv()读取.csv文件。其文件是 根据文件,我们知道: 数据类型:列的类型名称或目录->类型,默认无数据类型 用于数据或列。例如{'a':np.float64,'b':np.int32} (engine='python'不支持) 及 转换器:dict,默认无用于转换的函数dict 某些列中的值。键可以是整数或列 标签 使用此函数时,我可以调用 pandas.read_csv('file',dtype=object)或pandas.read_csv('file',converte

函数read_csv()读取.csv文件。其文件是

根据文件,我们知道:

数据类型:列的类型名称或目录->类型,默认无数据类型 用于数据或列。例如{'a':np.float64,'b':np.int32} (engine='python'不支持)

转换器:dict,默认无用于转换的函数dict 某些列中的值。键可以是整数或列 标签

使用此函数时,我可以调用
pandas.read_csv('file',dtype=object)
pandas.read_csv('file',converters=object)
。显然,converter,它的名称可以表示数据类型将被转换,但我想知道dtype的情况是什么?

语义上的区别在于
dtype
允许您指定如何处理值,例如,作为数字或字符串类型

转换器允许您使用转换函数解析输入数据以将其转换为所需的数据类型,例如,将字符串值解析为datetime或其他所需的数据类型

在这里,我们看到熊猫试图嗅出这些类型:

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
在这里,我们强制将
int
列设置为
str
,并告诉
parse\u dates
使用date\u解析器解析日期列:

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

我想说,
转换器的主要目的是操作列的值,而不是数据类型。@EdChum分享的答案集中在
dtypes
的概念上。它使用
pd.to\u datetime
函数

在本文中关于转换器的区域中,您将看到一个示例,该示例将csv列(值为“185磅”)更改为从文本列中删除“磅”的内容。这是
read\u csv
转换器
参数背后的更多想法

.csv的外观(如果图像未显示,请转到文章。)

在重量列上使用
转换器后的
数据帧。

我应该向其他人指出,这两个参数可以在同一个
读取csv
调用中提供,尽管我没有测试
dtypes
converter
字典中引用的重叠列(如果使用该API)。有没有办法让您自己的嗅探器转换dtypes?我觉得这对包含许多列的excel文件很有用。这是行不通的。excel工作表是使用第三方模块导入的,因此数据类型是通过该模块提供的。如果它是一个csv文件,那么您可以定义自己的函数并将其加载到转换器,并将其应用于每一列,这样就可以了
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
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
#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)