def stripUnderscores(structType, prefix=''): output = [] for name, field in zip(structType.names, structType.fields): if isinstance(field.dataType, T.StructType): col = F.struct(*stripUnderscores(field.dataType, prefix + name + '.')) else: col = F.col(prefix + name) output.append(col.alias(name.replace(' ', '_'))) return output