Python/pyspark 数据框重新排列列 [英] Python/pyspark data frame rearrange columns
本文介绍了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
Now I have to arrange the columns in such a way that the name
column comes after 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屋!
查看全文