类型错误:数组字段类别中的元素:无法合并类型<类'pyspark.sql.types.StringType'>和<class'pyspark.sql.types.DoubleType'>

2024-06-25 23:17:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我用Pandas读取csv文件,它是一个两列的数据帧,然后我试图转换成spark数据帧。代码是:

from pyspark.sql import SQLContext
sqlCtx = SQLContext(sc)
sdf = sqlCtx.createDataFrame(df)

数据帧:

^{pr2}$

给出了:

    Name    Category
0   EDSJOBLIST apply at www.edsjoblist.com  ['biotechnology', 'clinical', 'diagnostic', 'd...
1   Power Direct Marketing  ['advertising', 'analytics', 'brand positionin...
2   CHA Hollywood Medical Center, L.P.  ['general medical and surgical hospital', 'hea...
3   JING JING GOURMET   [nan]
4   TRUE LIFE KINGDOM MINISTRIES    ['religious organization']
5   fasterproms ['microsoft .net']
6   STEREO ZONE ['accessory', 'audio', 'car audio', 'chrome', ...
7   SAN FRANCISCO NEUROLOGICAL SOCIETY  [nan]
8   Fl Advisors ['comprehensive financial planning', 'financia...
9   Fortunatus LLC  ['bottle', 'bottling', 'charitable', 'dna', 'f...
10  TREADS LLC  ['retail', 'wholesaling']

有人能帮我吗?在


Tags: 文件csv数据代码frompandassqlnan
1条回答
网友
1楼 · 发布于 2024-06-25 23:17:53

Spark在处理object数据类型时会有困难。一个潜在的解决方法是先将所有内容转换为字符串:

sdf = sqlCtx.createDataFrame(df.astype(str))

这样做的一个结果是,包括nan在内的所有内容都将转换为字符串。您需要注意正确处理这些转换,并将列转换为适当的类型。在

例如,如果您有一个带浮点值的列"colA",您可以使用类似于以下的方法将字符串"nan"转换为null

^{pr2}$

相关问题 更多 >