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

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 :: 4D Array To DF 
Python :: ImportError: sys.meta_path is None, Python is likely shutting down 
Python :: np random list 
Python :: how to run test cases in python 
Python ::  
Python :: python - match two df on a variable with different name 
Python :: airflow schedule interval timezone 
Python ::  
Python :: date to timestamp python 
Python :: python default value 
Python :: star program in python using for loop 
Python :: df.pivot_table 
Python :: python type hints list of class 
Python ::  
Python :: quotation marks in string 
Python :: how to create an app under a folder in django 
Python :: np array size 
Python :: how to sort values in python 
Python :: python kivy bind 
Python :: Model In View Django 
Python :: Python Sum of an array in NumPy 
Python :: float python 
Python :: python if file exists append else create 
Python :: image hashing 
Python :: Broadcasting with NumPy Arrays Two dimension array dimension array Example 
Python :: how to declare a lambda function in python 
Python :: parse_dates 
Python :: python sort list opposite 
Python :: python class example 
Python :: puython is not equal to 
ADD CONTENT
Topic
Content
Source link
Name
4+4 =