pythoncsv数据类型,Python-如何获取CSV文件中所有列的数据类型?

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

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?

解决方案

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)

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.

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值