Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

convert dict to dataframe

#Lazy way to convert json dict to df

pd.DataFrame.from_dict(data, orient='index').T
Comment

turn df to dict

dict(df.values)
Comment

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 :: list files in http directory python 
Python :: python insert parent directory into sys path for import file purpose 
Python :: python get the intersection of two lists 
Python :: python absolute path 
Python :: Program to find GCD or HCF of two numbers python 
Python :: radiobuttons django 
Python :: remove prefix in python 3.6 
Python :: render django template 
Python :: python how to skip iteration 
Python :: python int to bytes 
Python :: selenium chrome options suppress warnings python 
Python :: pd.get_dummies 
Python :: sum of a numpy array 
Python :: how to convert csv to excel in python 
Python :: transform data frame in list 
Python :: bs4 innerhtml 
Python :: save object pickle python 
Python :: add item to python dictionary 
Python :: django group with permission 
Python :: qt designer messagebox python 
Python :: python add one month to a date 
Python :: count occurrences of character in string python using dictionary 
Python :: all() python 
Python :: scikit learn train test split 
Python :: ipynb import 
Python :: sklearn ridge regression 
Python :: matplotlib savefig legend cut off 
Python :: append two list of number to one python 
Python :: start process python 
Python :: python repet x time 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =