【发布时间】:2017-11-27 01:21:35
【问题描述】:
我能够在 DataFrame 中获取数据 from here。当我使用 graphlab 将 DataFrame 转换为 SFrame 时,会显示此错误:
TypeError: 无法从 float、string 类型推断出常见类型。
我的代码是
import graphlab
import pandas as pd
import numpy as np
import matplotlib.pyplot as pt
from numpy import array
from graphlab import SFrame
user_columns =['No','User id','User Name','Skill','Given Test Name','Given Test ID','Recommend Test','Recommend Test ID','Time Start','Time End','Score','Max Score']
data=pd.read_table('http://tech4partner.com.cp-in-5.webhostbox.net/recommend_test.php', sep='^',header=None,names = user_columns)
df2 = data.ix[2:]
df3=df2.reset_index()
df4=df3.drop('index', axis=1)
df5=df4.drop(df4.index[len(df4)-1])
df5=df5.reset_index(1,[len(df5)-1])
df7 = df5.reset_index()
df8=df7.drop('index', axis=1)
df9=df8.head(len(df8))
df9.index = np.arange(1, len(df9) + 1)
df9=df9.head(50)
df9
数据在这一行的 DataFrame 中显示。当我将 DataFrame 转换为 Sframe 时,它会出错。
DataFrame 使用 .Sframe() 方法转换为 SFrame
t_data = graphlab.SFrame(df9)
t_data
根据我的说法,数据格式正确,也可能不是。我也尝试了两种解决方案,但对我不起作用。
1)我检查了每一列,但没有一列具有不同的数据类型。
2)我使用
转换了特定数据类型的每一列df.column_name.astype(str) 和 df['column_name']= df[coulmn_name].astype(str)
但它不再起作用了。
TypeError Traceback (most recent call last)
<ipython-input-11-c852cd1e7f45> in <module>()
----> 1 t_data = graphlab.SFrame(df9)
2 t_data
3
C:\Anaconda2\lib\site-packages\graphlab\data_structures\sframe.pyc in __init__(self, data, format, _proxy)
951 pass
952 else:
--> 953 raise ValueError('Unknown input type: ' + format)
954
955 sframe_size = -1
C:\Anaconda2\lib\site-packages\graphlab\cython\context.pyc in __exit__(self, exc_type, exc_value, traceback)
47 if not self.show_cython_trace:
48 # To hide cython trace, we re-raise from here
---> 49 raise exc_type(exc_value)
50 else:
51 # To show the full trace, we do nothing and let exception propagate
**TypeError: A common type cannot be infered from types float, string.**
如何将此 DataFrame 转换为 SFrame?
【问题讨论】:
-
请阅读Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers? - 总结是这不是解决志愿者的理想方式,并且可能会适得其反。请不要将此添加到您的问题中。
-
感谢您的评论,如果您觉得我写错了“紧急”和“尽快”,那么对不起。我不介意你可以否决我的问题。
-
让我告诉你一件事,我在 Anaconda 中使用 Jupyter。我已经在 Jupyter 中制作了一个脚本,其中 DataFrame 很容易转换为 SFrame 而没有任何麻烦。Thanx Halfer
标签: python python-2.7 python-3.x pandas graphlab