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 :: geopandas read postgis SQL 
Python :: how to play audio in python using pygame 
Python :: pandas df count values less than 0 
Python :: python save images from directory to list 
Python :: how to repeat a row in pandas 
Python :: how delete an entry tkinter 
Python :: pip not recognized 
Python :: tkinter change button foreground 
Python :: python generate tuple from lists 
Python :: split list python percent 
Python :: lru_cache 
Python :: change xlabel python 
Python :: every second value python 
Python :: string format method python 
Python :: pandas get higher value of column 
Python :: python find minimum date in list 
Python :: How to Get the Symmetric Difference of Sets in Python 
Python :: how to change an integer to a string in python permanently 
Python :: access element from list python 
Python :: best python books python 3 
Python :: protected vs private python 
Python :: sorted string 
Python :: Python Iterating Through an Iterator 
Python :: how to make every item compare the rest items of list in python 
Python :: only split from third delimiter python 
Python :: pandas set hour for timestamp 
Python :: primes python 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =