本质上这是比较两个数据框,我能够将它们的名称与:
def diff(first, second):
second = set(second)
return [item for item in first if item not in second]
但是我不仅要比较名称,还要比较数据类型
数据帧示例如下:
>>> pDF1.schema
StructType(
List(
StructField(Scen_Id,IntegerType,true),
StructField(Flow_Direction,StringType,true),
StructField(Dataset_Type,StringType,true),
StructField(Flag_Extrapolation_Percent_Change_Stay,IntegerType,true)
)
)
>>> pDF2.schema
StructType(
List(
StructField(Scen_Id,StringType,true),
StructField(Flow_Direction,StringType,true),
StructField(Dataset_Type,StringType,true),
StructField(Flag_Extrapolation_Percent_Change_Stay,IntegerType,true)
)
)
从这个简化的示例中可以看到(通常情况下,我们的数据框包含100多个字段),pDF2与pDF1具有相同的名称/数据类型,除了第一个字段具有不同的数据类型。
非常感谢您。
答案 0 :(得分:0)
好的,所以答案的确非常简单,如下所示,供以后的读者参考:
def diff(first, second):
second = set(second)
return [item for item in first if item not in second]
dl1_fields = list(pDF1.schema.fields)
dl2_fields = list(pDF2.schema.fields)
print("=========================================================")
print("schema comparison result:")
print("=========================================================")
dl1Notdl2 = diff(dl1_fields, dl2_fields)
print(str(len(dl1Notdl2)) + " columns in first df but not in second")
pprint.pprint(dl1Notdl2)
print("=========================================================")
dl2Notdl1 = diff(dl2_fields, dl1_fields)
print(str(len(dl2Notdl1)) + " columns in second df but not in first")
pprint.pprint(dl2Notdl1)