Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

dataframe to dictionary

>>> df.to_dict('records')
[{'col1': 1, 'col2': 0.5}, {'col1': 2, 'col2': 0.75}]
Comment

convert a dictionary to Pandas DataFrame

import pandas as pd

my_dict = {key:value,key:value,key:value,...}
df = pd.DataFrame(list(my_dict.items()),columns = ['column1','column2'])
Comment

pandas to dictionary

df.to_dict('records')
Comment

pandas dataframe.to_dict

#orientstr {‘dict’, ‘list’, ‘series’, ‘split’, ‘records’, ‘index’}
#Determines the type of the values of the dictionary.
#‘dict’ (default) : dict like {column -> {index -> value}}
#‘list’ : dict like {column -> [values]}
#‘series’ : dict like {column -> Series(values)}
#‘split’ : dict like {‘index’ -> [index], ‘columns’ -> [columns], ‘data’ -> [values]}
#‘records’ : list like [{column -> value}, … , {column -> value}]
#‘index’ : dict like {index -> {column -> value}}

# Example:
data = pandas.read_csv("data/data_name.csv")
to_dict = data.to_dict(orient="records")
Comment

convert pandas dataframe to dict with a column as key

df.set_index('columnName').T.to_dict()
Comment

pandas df to dict

df.to_dict
Comment

from pandas to dictionary

df = pd.DataFrame({'col1': [1, 2],
...                    'col2': [0.5, 0.75]},
...                   index=['row1', 'row2'])
>>> df
      col1  col2
row1     1  0.50
row2     2  0.75
>>> df.to_dict()
{'col1': {'row1': 1, 'row2': 2}, 'col2': {'row1': 0.5, 'row2': 0.75}}
Comment

df to dict

>>> df.set_index('ID').T.to_dict('list')
{'p': [1, 3, 2], 'q': [4, 3, 2], 'r': [4, 0, 9]}
Comment

PREVIOUS NEXT
Code Example
Python :: tensorflow adam 
Python :: abc python 
Python :: python how to calculate how much time code takes 
Python :: pyqt open file dialog 
Python :: pyspark rdd filter 
Python :: ++ python 
Python :: plot a circle in python using equation of a circle 
Python :: 3 dimensional array in numpy 
Python :: pandas dataframe add column from another column 
Python :: kivymd window size 
Python :: import csrf_exempt django 
Python :: hot to check tkinter verionin python 
Python :: python lambda 
Python :: get the current date and time in python 
Python :: how to get the ip address of laptop with python 
Python :: python argparse file argument 
Python :: Python t date from a timestamp 
Python :: can list comprehenios contain else 
Python :: give a function a name python 
Python :: how to scrape multiple pages using selenium in python 
Python :: progress bar in cmd python 
Python :: python null 
Python :: how to remove duplicates from a python list 
Python :: notion python api 
Python :: python if not null or empty 
Python :: kivy button on click 
Python :: how to count how many cameras you have with python 
Python :: Python NumPy split Function Example 
Python :: create a dictionary from a list python 
Python :: bucketizer pyspark 
ADD CONTENT
Topic
Content
Source link
Name
3+2 =