Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python/pyspark data frame rearrange columns

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

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

Now I have to arrange the columns in such a way that the name column comes after id

I have done like below

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] 

I am getting this error

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.

like image 864
User12345 Avatar asked Mar 20 '17 19:03

User12345


People also ask

How do you interchange columns in PySpark?

You can replace column values of PySpark DataFrame by using SQL string functions regexp_replace(), translate(), and overlay() with Python examples.

How do I sort column names in PySpark DataFrame?

You can use either sort() or orderBy() function of PySpark DataFrame to sort DataFrame by ascending or descending order based on single or multiple columns, you can also do sorting using PySpark SQL sorting functions, In this article, I will explain all these different ways using PySpark examples.

How do I change the order of columns in a Pandas DataFrame?

You can change the order of columns in the pandas dataframe using the df. reindex() method.

How do you add a column in PySpark DataFrame at a specific position?

In PySpark, to add a new column to DataFrame use lit() function by importing from pyspark. sql. functions import lit , lit() function takes a constant value you wanted to add and returns a Column type, if you wanted to add a NULL / None use lit(None) .


2 Answers

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

df.select("id","name","time","city") 
like image 127
Alex Avatar answered Sep 29 '22 07:09

Alex


If you're working with a large number of columns:

df.select(sorted(df.columns)) 
like image 37
melchoir55 Avatar answered Sep 29 '22 09:09

melchoir55