Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pyspark dataframe to dictionary

import numpy as np
  
# Convert the dataframe into list
# of rows
rows = [list(row) for row in df.collect()]
  
# COnvert the list into numpy array
ar = np.array(rows)
  
# Declare an empty dictionary
dict = {}
  
# Get through each column
for i, column in enumerate(df.columns):
  
    # Add ith column as values in dict
    # with key as ith column_name
    dict[column] = list(ar[:, i])
  
# Print the dictionary
print(dict)
Comment

PREVIOUS NEXT
Code Example
Python :: python prettytable 
Python :: python clear stdout 
Python :: django class based views 
Python :: open image in PILLOW 
Python :: AI chatbot in Python - NAYCode.com 
Python :: download files from url in flask 
Python :: Python __mul__ magic method 
Python :: class inside class python 
Python :: pandas aggregate dataframe 
Python :: How to Send WhatsApp API using python 
Python :: python internship 
Python :: pygame scroll event 
Python :: merge two sorted lists into one sorted list 
Python :: multiple inputs in one line- python 
Python :: python django query 
Python :: join two querysets django 
Python :: how to delete record in django 
Python :: cookies in django 
Python :: data frame 
Python :: join in pathlib path 
Python :: np.vectorize 
Python :: Read excel formula value python openpyxl 
Python :: sets in python 
Python :: promises in python 
Python :: python = align 
Python :: flask delete from database 
Python :: list to text python 
Python :: how to find missing item in a list 
Python :: python format string 
Python :: python list of size 
ADD CONTENT
Topic
Content
Source link
Name
7+3 =