Python 将pandas数据帧转换为PySpark RDD时出现问题?

Python 将pandas数据帧转换为PySpark RDD时出现问题?,python,python-2.7,pandas,pyspark,pyspark-sql,Python,Python 2.7,Pandas,Pyspark,Pyspark Sql,使用pandasread_csv()函数,我读取了一个iso-8859-1文件,如下所示: df = pd.read_csv('path/file', \ sep = '|',names =['A','B'], encoding='iso-8859-1') from pyspark.sql import SQLContext spDF = sqlContext.createDataFrame(df['A']) spDF.show() 然后,我想使用MLL

使用pandas
read_csv()
函数,我读取了一个
iso-8859-1
文件,如下所示:

df = pd.read_csv('path/file', \
                   sep = '|',names =['A','B'], encoding='iso-8859-1')
from pyspark.sql import SQLContext
spDF = sqlContext.createDataFrame(df['A'])
spDF.show()
然后,我想使用MLLib的word2vect。但是,它只接受RDDs作为参数。因此,我尝试将pandas数据帧转换为RDD,如下所示:

df = pd.read_csv('path/file', \
                   sep = '|',names =['A','B'], encoding='iso-8859-1')
from pyspark.sql import SQLContext
spDF = sqlContext.createDataFrame(df['A'])
spDF.show()
无论如何,我得到了以下例外:

TypeError: Can not infer schema for type: <type 'unicode'>
TypeError: Can not infer schema for type: <type 'unicode'>
然后:

我也得到了同样的例外:

TypeError: Can not infer schema for type: <type 'unicode'>
TypeError: Can not infer schema for type: <type 'unicode'>
TypeError:无法推断类型的架构:

当您使用
df['A']
时,它不是
pandas.DataFrame
而是
pandas.Series
,因此当您将它传递给
SqlContext.createDataFrame
时,它会被视为任何其他
Iterable
,PySpark不支持将简单类型转换为
DataFrame

如果要将数据保留为
DataFrame
格式,请使用
loc
方法:

df.loc[:,'A']

从@zeros323答案中,我注意到它实际上不是一个数据帧。我发现
to_frame()
可以转换数据帧中的特定列。因此,我做了以下工作:

new_dataframe = df['A'].to_frame()
new_dataframe.head()
from pyspark.sql import SQLContext
spDF = sqlContext.createDataFrame(new_dataframe)
spDF.show()