繁体   English   中英

如何在Pyspark中的数据框中删除utf格式并将列从字符串转换为Integer

[英]How to remove utf format in a dataframe in Pyspark and convert column from string to Integer

我需要删除utf格式并将列转换为Integer类型。

以下是我为删除utf格式所做的工作

>>auction_data = auction_raw_data.map(lambda line: line.encode("ascii","ignore").split(","))
>>auction_Data.take(2)
>>[['8211480551', '52.99', '1.201505', 'hanna1104', '94', '49.99', '311.6'], ['8211480551', '50.99', '1.203843', 'wrufai1', '90', '49.99', '311.6']]

但是,当我使用模式为相同数据创建数据帧并尝试检索特定数据时,我得到前缀为“u”的数据。

>>schema = StructType([ StructField("auctionid", StringType(), True),
StructField("bid", StringType(), True),
StructField("bidtime", StringType(), True),
StructField("bidder", StringType(), True),
StructField("bidderrate", StringType(), True),
StructField("openbid", StringType(), True),
StructField("price", StringType(), True)])`  

>>xbox_df = sqlContext.createDataFrame(auction_data,schema)
>>xbox_df.registerTempTable("auction")
>>first_line = sqlContext.sql("select * from auction where auctionid=8211480551").collect()
>>for i in first_line:
>>   print i

>>Row(auctionid=u'8211480551', bid=u'52.99', bidtime=u'1.201505', bidder=u'hanna1104', bidderrate=u'94', openbid=u'49.99', price=u'311.6')
>>Row(auctionid=u'8211480551', bid=u'50.99', bidtime=u'1.203843', bidder=u'wrufai1', bidderrate=u'90', openbid=u'49.99', price=u'311.6')

如何删除u'值的前面,我也想将出价值转换为整数。 当我直接更改模式定义时,我收到错误说“TypeError:IntegerType不能接受类型中的对象”。显示更少

我正在加载JSON而不使用模式,所以我不知道是否存在差异。 使用select时将字段转换为int时没有问题。 这就是我做的:

from pyspark.sql.functions import *
...
df = df.select(col('intField').cast('int'))
df.show()
# prints Row(intField=123)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM