熊猫read_csv按列索引(而不是名称)设置"dtype"



file.txt有一个标题和四列。但是标题一直在变化。

像这样:

,'non_standard_header_1','non_standard_header_2','non_standard_header_3'
,kdfjlkjdf, sdfdfd,,
,kdfjlkjwwdf, sdfddffd,,
,kdfjlkjwwdf,, sdfddffd,

我想在熊猫中导入file.txt,并且我希望将列作为object导入。直观的方法(对我来说(:

dtype = [object, object, object]如下:

daily_file              = pandas.read_csv('file.txt',
usecols      = [1, 2, 3],
dtype        = [object, object, object])

不起作用,运行上述操作,我得到:

data type not understood

如何在没有引用(现有(列名的导入时设置列dtype

pd.read_csv(..., dtype=object)将在所有读入的列中全局应用对象 dtype,如果这是您要查找的内容。

否则,如果要将 dtypes 映射到列名,则需要传递形式{'col' : dtype}字典。

最新更新