尝试用以下格式将csv文件读入数据
dp = pd.read_csv('products.csv', header = 0, dtype = {'name': str,'review': str,
'rating': int,'word_count': dict}, engine = 'c')
print dp.shape
for col in dp.columns:
print 'column', col,':', type(col[0])
print type(dp['rating'][0])
dp.head(3)
这是输出:
(183531, 4)
column name : <type 'str'>
column review : <type 'str'>
column rating : <type 'str'>
column word_count : <type 'str'>
<type 'numpy.int64'>
我可以理解可能会发现很难将字典的字符串表示转换为this和this的字典。但是,“评级”栏的内容如何既包括str,又包括numpy.int64?
顺便说一句,像不指定引擎或标题这样的调整不会改变任何事情。
感谢并致以问候
发布于 2016-03-24 08:11:54
在您的循环中,您正在执行以下操作:
for col in dp.columns:
print 'column', col,':', type(col[0])
您在任何地方都正确地将str
看作输出,因为col[0]
是列名的第一个字母,它是一个字符串。
例如,如果运行此循环:
for col in dp.columns:
print 'column', col,':', col[0]
您将看到每个列名的字符串的第一个字母被打印出来--这就是col[0]
。
循环只对列名进行迭代,而不对系列数据进行迭代。
您真正想要的是在一个循环中检查每个列的数据类型(不是它的头或者它的部分头)。
因此,这样做可以获得列数据的类型(非标头数据):
for col in dp.columns:
print 'column', col,':', type(dp[col][0])
这类似于单独打印rating
列类型时所做的工作。
发布于 2016-03-24 08:19:06
使用:
dp.info()
若要查看列的数据类型,请执行以下操作。dp.columns
引用列标题名称,它们是字符串。
发布于 2016-03-24 08:01:54
我想你应该先检查一下:Pandas: change data type of columns
当谷歌pandas dataframe column type
,它在前五名的答案。
https://stackoverflow.com/questions/36195485
复制相似问题