我正在从excel中读取excel工作表,我需要将该数据作为json存储在HDFS中。对于一些床单我面临例外
excel_file = pd.ExcelFile("export_n_moreExportData10846.xls")
for sheet_name in excel_file.sheet_names:
df = pd.read_excel(excel_file, header=None, squeeze=True, sheet_name=sheet_name)
if sheet_name=='Passed':
print '**************' + sheet_name + '******************'
for i, row in df.iterrows():
data = df.iloc[(i+1):].reset_index(drop=True)
data.columns = pd.Series(list(df.iloc[i])).str.replace(' ','_')
break
for c in data.columns:
data[c] = pd.to_numeric(data[c], errors='ignore')
print data #I'm able to print the data
result1 = sparkSession.createDataFrame(data) #Facing the exception here
print "inserting data into HDFS..."
result1.write.mode("append").json(hdfsPath)
print "inserted data into hdfs"
我面临以下例外
raise TypeError("Can not merge type %s and %s" % (type(a), type(b)))
TypeError: Can not merge type <class 'pyspark.sql.types.StringType'> and <class 'pyspark.sql.types.DoubleType'>
图中显示了数据
这可能是因为某些列在同一列中有不同的数据类型,pandas可以处理('object'类型),spark-df不能。你知道吗
有几种处理方法:
你可以跳过spark-df阶段,把df转换成dicts(数据框到目录(orient='records')并将其读取到RDD并保存(考虑使用json加载和转储转换为正确的json)。
将对象列强制转换为字符串(df[col]=df[col].astype(str))。
取决于你到底想要什么。你知道吗
为了这个数据.fillna('0',inplace=True)工作,因为列有空记录。你知道吗
相关问题 更多 >
编程相关推荐