简体   繁体   English

如何确定 python 中 csv 文件中列的数据类型?

[英]How to determine the datatype of columns in a csv file in python?

I am trying to determine the data type of all the columns in a csv file.我正在尝试确定 csv 文件中所有列的数据类型。 The code is:代码是:

df = pd.read_csv('accepted.csv')
for name, dtype in df.dtypes.iteritems():
        print("Column Name: ",name," ||| ","Data Type : ", dtype)

Now the problem I am facing is that it gives float64 for most of the columns and 0 for some.现在我面临的问题是它为大多数列提供float64,而为某些列提供0。 How can I get the original data type?如何获取原始数据类型?

@Gandharv this code is working fine, i got the correct data types for each features However you can directly view the datatypes using.dtypes(): This returns a Series with the data type of each column. @Gandharv 此代码工作正常,我为每个功能获得了正确的数据类型但是您可以使用.dtypes() 直接查看数据类型:这将返回一个包含每列数据类型的系列。 Why do you need to use for loop for this since we've a function to do it.为什么你需要使用 for 循环,因为我们有一个 function 来做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Python中确定CSV文件中的列数 - How to determine the number of columns in a CSV file in Python 如何比较csv文件的三列并使用python确定丢失的数据? - How to compare three columns of a csv file and determine the missing data with python? 如何在Python中确定数据类型? - How to determine the datatype in Python? 如何将 csv 文件保存在 python 中,其中一个特定列的数据类型必须是 int32? - How to save csv file in python with datatype of one particular columns has to be int32? 如何在 Python 中从 CSV 文件中读取列 - How to read columns from CSV file in Python 如何通过python中的两列对csv文件进行排序? - how to sort a csv file by two columns in python? 如何将 CSV 文件的列导出为 python 中的 arrays? - how to export a columns of CSV file as an arrays in python? 如何将列表数据类型和整数数据类型保存到单个 .csv 文件中 - How to save List datatype and integer datatype into single .csv file 如何根据列索引列表从 pyspark 中的 csv 文件中选择某些列,然后确定它们的不同长度 - How to select certain columns from a csv file in pyspark based on the list of index of columns and then determine their distinct lengths 如何确定 CSV 文件的编码? - How to determine the encoding of a CSV file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM