我正在将 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?
np.bool_ 和 pd.tslib.Timestamp 没有运气.np.bool_ and pd.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模板网!
如何将函数绑定到 Qt 菜单栏中的操作?How to bind a function to an Action from Qt menubar?(如何将函数绑定到 Qt 菜单栏中的操作?)
PyQt 启动后进度跃升至 100%PyQt progress jumps to 100% after it starts(PyQt 启动后进度跃升至 100%)
如何将 yaxis 刻度标签设置在固定位置,以便当我How to set yaxis tick label in a fixed position so that when i scroll left or right the yaxis tick label should be visible?(如何将 yaxis 刻度标签设
`QImage` 构造函数有未知关键字 `data``QImage` constructor has unknown keyword `data`(`QImage` 构造函数有未知关键字 `data`)
将 x 轴刻度更改为自定义字符串Change x-axis ticks to custom strings(将 x 轴刻度更改为自定义字符串)
如何在python中将文件保存为excel时显示进度条?How to show progress bar while saving file to excel in python?(如何在python中将文件保存为excel时显示进度条?)