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 ?
PySpark:
sorted(df.columns)
Scala:
df.columns.sorted
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With