TypeError:在Pyspark上的DF列上运行select时,“NoneType”对象不可编辑

2024-04-29 14:32:39 发布

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

我正在使用Pyspark分析文件-https://www.kaggle.com/rounakbanik/the-movies-dataset?select=movies_metadata.csv

它有一个json列,如“生产公司”、“生产国”。我解析了这些列,并使用pyspark代码提取了“name”,如下所示

    schema = ArrayType(StructType([
        StructField('id', IntegerType(), nullable=False), 
        StructField('name', StringType(), nullable=False)]))

convertUDF = udf(lambda s: ','.join(map(str, s)),StringType())

df2=df2.withColumn("production_companies_values",when(col('production_companies')=='[]','YU').otherwise(convertUDF(from_json(movies_metadata.production_companies,schema).getField("name"))))\
       .withColumn("production_countries_values",convertUDF(from_json(movies_metadata.production_countries,schema).getField("name")))\
       .withColumn("genres_value",convertUDF(from_json(movies_metadata.genres,schema).getField("name")))
df2.show()

我得到的错误是-TypeError:“NoneType”对象在按列进行分组时是不可编辑的-production\u companys\u值

df2.groupBy("production_companies_values").count().show()

show()显示列被正确解析


Tags: namefromjsonschemashowmoviesproductionmetadata