Python-如何获取CSV文件中所有列的数据类型? [英] Python - How to get data types for all columns in CSV file?

查看:1239
本文介绍了Python-如何获取CSV文件中所有列的数据类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从CSV文件中获取每一列的所有数据类型.
没有文件中有关数据类型的文档,手动检查将花费很长时间(它有150列).

开始使用这种方法:

I am trying to get all data types from a CSV file for each column.
There is no documentation about data types in a file and manually checking will take a long time (it has 150 columns).

Started using this approach:

df = pd.read_csv('/tmp/file.csv')

>>> df.dtypes
a   int64
b   int64
c   object
d   float64

以上方法是否足够好,还是有更好的方法找出数据类型?
另外-文件有150列.当我键入df.types时-我只能看到15个左右的列.怎么看全部?

Is above approach good enough or there is a better approach to figure out data types?
Also - file has 150 columns. When I type df.types - I can see only 15 or so columns. How to see them all?

推荐答案

根据文件的大小,您可以通过使用nrows自变量来读入前几行,从而节省一些时间. a href ="https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html" rel ="nofollow noreferrer"> pd.read_csv :

Depending on the size of your file, you might be able to save some time by only reading in the first few rows, using the nrows argument of pd.read_csv:

df = pd.read_csv('/tmp/file.csv', nrows=25)

仅当您确定可以从前n行正确推断出类型时,此功能才有用,因此请务必小心.

This is only useful if you know for sure that the types can be correctly inferred from the first n rows though, so be careful with this.

将数据(或其子集)加载到DataFrame中之后,您可以通过多种不同的方式查看类型,其中一些已经发布过,但是我将使用一种简单的方法来共享另一种类型.循环并 iteritems :

Once you have the data (or a subset of it) loaded into a DataFrame, you can view the types in a number of different ways, a few of which have been posted already, but I'll share another using a simple loop and iteritems:

for name, dtype in df.dtypes.iteritems():
    print(name, dtype)

a int64
b float64
c object

这篇关于Python-如何获取CSV文件中所有列的数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆