Python/Pyspark数据框重新排列列 [英] Python/pyspark data frame rearrange columns

查看:503
本文介绍了Python/Pyspark数据框重新排列列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在python/pyspark中有一个数据框,其列为id time city zip等...

I have a data frame in python/pyspark with columns id time city zip and so on......

现在,我在此数据框中添加了新列name.

Now I added a new column name to this data frame.

现在我必须以name列在id

我已经完成了以下操作

change_cols = ['id', 'name']

cols = ([col for col in change_cols if col in df] 
        + [col for col in df if col not in change_cols])

df = df[cols]

我收到此错误

pyspark.sql.utils.AnalysisException: u"Reference 'id' is ambiguous, could be: id#609, id#1224.;"

为什么会出现此错误.我该如何纠正.

Why is this error occuring. How can I rectify this.

推荐答案

您可以使用select更改列的顺序:

You can use select to change the order of the columns:

df.select("id","name","time","city")

这篇关于Python/Pyspark数据框重新排列列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆