I am trying to run pyspark script. In this script i am executing sql query and creating dataframe after i am trying to change last column position into first but it is giving me error. Can you please help me how to do this?
code:
if(masterjobname.endswith('ADDR_Phani')): df = sqlContext.sql(query) print("Target: "+targetpath ) w = (Window().orderBy("SOURCE_COLUMN_VALUE")) df = df.withColumn("SYSTEM_ID", dense_rank().over(w)) cols = df.columns.tolist() cols = cols[-1:] + cols[:-1] df = df[cols]
score:0
No matter if your variable is or isn't a list:
list(var)
You can use it as exception handling; i.e. ensure that it is indeed a list object.
score:1
Okay i got the answer.
df.columns.values.tolist()
gives the list of the columns.
solution:
if(masterjobname.endswith('ADDR_Phani')):
df = sqlContext.sql(query)
print("Target: "+targetpath )
w = (Window().orderBy("SOURCE_COLUMN_VALUE"))
df = df.withColumn("SYSTEM_ID", dense_rank().over(w))
cols = df.columns.values.tolist()
cols = cols[-1:] + cols[:-1]
df = df[cols]
Credit To: stackoverflow.com
Related Query
- Getting 'list' object has no attribute 'tolist' in python
- Getting AttributeError: 'list' object has no attribute 'split' when using list comprehension
- Trying to send emails from python using smtplib and email.mime.multipart, getting the error "'Series' object has no attribute 'encode'"
- FigureCanvasAgg' object has no attribute 'invalidate' ? python plotting
- Type Conversion in python AttributeError: 'str' object has no attribute 'astype'
- Python Pandas Group By Error 'Index' object has no attribute 'labels'
- Python Pandas - Index' object has no attribute 'hour'
- Python AttributeError: 'str' object has no attribute 'DataFrame'
- feather data storage library for python 'module' object has no attribute 'write_dataframe' error
- Getting AttributeError 'Workbook' object has no attribute 'add_worksheet' - while writing data frame to excel sheet
- Python Pandas: Resolving "List Object has no Attribute 'Loc'"
- Python / Numpy AttributeError: 'float' object has no attribute 'sin'
- Python Pandas: 'numpy.ndarray' object has no attribute 'apply'
- Getting "AttributeError: 'float' object has no attribute 'replace'" error while replacing string
- Getting error AttributeError: 'bool' object has no attribute 'transpose' when attempting to fit machine learning model
- python pandas- AttributeError: 'Series' object has no attribute 'columns'?
- AttributeError: 'DataFrame' object has no attribute 'colmap' in Python
- Python broke, AttributeError: 'module' object has no attribute 'python_version'
- Error in reading html to data frame in Python "'module' object has no attribute '_base'"
- Python Pandas to_datetime AttributeError: 'tuple' object has no attribute 'lower'
- AttributeError: 'list' object has no attribute 'keys' when attempting to create DataFrame from list of dicts
- Python Pandas: AttributeError: 'str' object has no attribute 'loc'
- Python loop through Dataframe 'Series' object has no attribute
- getting this on dataframe 'int' object has no attribute 'lower'
- 'DataFrame' object has no attribute 'tolist' when I try to convert an excel file to a list
- Python AttributeError: 'Series' object has no attribute 'isdigit'
- Python Pandas Error: AttributeError: 'module' object has no attribute 'formats'
- Getting 'SparseDtype' object has no attribute 'itemsize' when trying to save sparse df to hdf5?
- Python - Pandas: AttributeError: 'numpy.ndarray' object has no attribute 'start'
- Getting 'Series' object has no attribute 'isnumeric' while filtering data in pandas
More Query from same tag
- How to iterate and sample from each category of a dataframe?
- Stacked bar plot disconnected
- Group nearby column values in Python Dataframe
- How to segment time series data into 3 column and 3 channels?
- Create linear model to check correlation tokenize error
- resample a start & end employee holiday table correctly
- Groupby cumulative sum in pandas based on specific condition
- Append two columns into one and separate them with an empty row pandas
- Pandas join by greater than or less than
- How to merge two pandas DataFrames and keep repeated values?
- Which is the most efficient way of flattening down a pandas dataframe?
- Iterating through a Pandas DataFrame is the same as Iterating through its Column-Names?
- Python, Zeep response to pandas
- Preserving the index when selecting a slice of a pandas dataframe
- Pandas dataframe top users by amount within 2016