我正在尝试从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
要将pandas数据帧转换为pyspark数据帧,请尝试以下操作
相关问题 更多 >
编程相关推荐