如何在PySpark中使用StructType将浮点数转换为IntegerType?

2024-10-01 11:35:21 发布

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

我正在尝试从Pandas数据帧创建Spark数据帧,其中我使用StructType类指定列数据类型。我已经将pandas数据帧保存为df,将spark数据帧保存为data。你知道吗

在我进入这些之前,csv文件中的某个地方有一个错误,我使用了pandas的read\u csv方法的参数error\u bad\u lines。我不熟悉火花当量。你知道吗

df = pd.read_csv('Amazon_Responded_Oct05.csv',error_bad_lines=False)
df.head()
>>>>
    user_id_str user_followers_count    text_
0   143515471.0 1503    @AmazonHelp Can you please DM me? A product I ...
1   85741735.0  149569  @SeanEPanjab I'm sorry, we're unable to DM you...
2   143515471.0 1503    @AmazonHelp It was purchased on... 
3   143515471.0 1503    @AmazonHelp I am following you now, if it help...
4   85741735.0  149569  @SeanEPanjab Please give us a call/chat so we ...

注意user\u id\u str列是如何用浮点值填充的,下面的143515471.0就是引发错误的地方。你知道吗

data_schema = [StructField('user_followers_count',IntegerType(),True),
               StructField('user_id_str',StringType(),True),
               StructField('text',StringType(),True)]
final_struc = StructType(fields=data_schema)

data = spark.createDataFrame(df,schema=final_struc)
>>>>
TypeError: field user_followers_count: IntegerType can not accept object 143515471.0 in type <class 'float'>

我试着从最后一刻开始补救,但没有成功

df.astype({'user_id_str': 'int','user_followers_count':'int','text_':'str'}).dtypes
df.head(1)
>>>>
    user_id_str user_followers_count    text_
0   143515471.0 1503    @AmazonHelp Can you please DM me? A product I ...

总之,我已经采取了各种方法来实现我的目标,创建了一个包含列数据类型、IntegerType、IntegerType和StringType的Spark数据帧,但没有成功。我非常希望能有一种方法来强制这种数据转换。你知道吗

编辑:

最后,我试着简单地从Spark开始;但那也是徒劳的。你知道吗


data_1 = spark.read.csv('Amazon_Responded_Oct05.csv',schema=final_struc,enforceSchema=True)
data_1.head(5)
>>>>
+--------------------+-----------+----+
|user_followers_count|user_id_str|text|
+--------------------+-----------+----+
|                null|       null|null|
|                null|       null|null|
|                null|       null|null|
|                null|       null|null|
|                null|       null|null|
+--------------------+-----------+----+
only showing top 5 rows

Tags: csv数据textyouiddfdataschema
1条回答
网友
1楼 · 发布于 2024-10-01 11:35:21

要将pandas数据帧转换为pyspark数据帧,请尝试以下操作

from pyspark.sql import Row
import pandas as pd
from pyspark.sql.types import StructField, StructType, StringType, IntegerType

#create a sample pandas dataframe
data = {'a':['hello', 'hi', 'world'], 'b':[5.0, 6.4, 9.7], 'c':[1,2,3]}
df = pd.DataFrame(data)
'''
    a       b       c
0   hello   5.0     1
1   hi      6.4     2
2   world   9.7     3
'''

#convert second column type to integer
df = df.astype({'b':'int'})
df
'''
    a       b       c
0   hello   5       1
1   hi      6       2
2   world   9       3
'''

#prepare the schema
fields = [StructField('a',StringType(),True),\
               StructField('b',IntegerType(),True),\
               StructField('c',IntegerType(),True)]
schema = StructType(fields)


#convert to a pyspark dataframe
rows = [Row(**_) for _ in df.to_dict(orient='records')]
#[Row(a='hello', b=5, c=1), Row(a='hi', b=6, c=2), Row(a='world', b=9, c=3)]
df_sp = spark.createDataFrame(rows, schema)
df_sp.show()
# +  -+ -+ -+
# |    a|  b|  c|
# +  -+ -+ -+
# |hello|  5|  1|
# |   hi|  6|  2|
# |world|  9|  3|
# +  -+ -+ -+

相关问题 更多 >