【问题标题】:TypeError: A common type cannot be infered from types float, string in pythonTypeError:无法从 python 中的 float、string 类型推断出常见类型
【发布时间】: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?

【问题讨论】:

标签: python python-2.7 python-3.x pandas graphlab


【解决方案1】:

试试这个语法进行转换:

df['column_name'] = df['column_name'].astype(float)
df['column_name'] = df['column_name'].astype(str)

【讨论】:

  • 我也使用了这种语法..它对我不起作用..现在我编辑了我的问题并分享了完整的代码,如您所见....
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-20
  • 1970-01-01
  • 2023-03-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多