Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you display Dataframe column names sorted?

I have a Dataframe that has over 250 columns. I can display the columns using df.printSchema or I can get it using df.columns, is there a way to get column names (just the column names - not the content of columns)in sorted order asc/desc ?

like image 721
Bala Avatar asked Oct 23 '17 19:10

Bala


Video Answer


2 Answers

PySpark:

sorted(df.columns)

Scala:

df.columns.sorted
like image 100
MaxU - stop WAR against UA Avatar answered Oct 24 '22 00:10

MaxU - stop WAR against UA


If we also need to view the data type along with sorted by column name :

sorted(df.dtypes)

df.dtypes - returns an array of tuples [(column_name, type), (column_name, type)...]

sorted - by default will sort by the first value in each tuple. So we will get the desired result of sorting by column names and get type of each column as well.

like image 40
Sairam Krish Avatar answered Oct 24 '22 02:10

Sairam Krish