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

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 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

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

pandas use dict to transform entries

>>> df = pd.DataFrame({'col2': {0: 'a', 1: 2, 2: np.nan}, 'col1': {0: 'w', 1: 1, 2: 2}})
>>> di = {1: "A", 2: "B"}
>>> df
  col1 col2
0    w    a
1    1    2
2    2  NaN
>>> df.replace({"col1": di})
  col1 col2
0    w    a
1    A    2
2    B  NaN
Comment

PREVIOUS NEXT
Code Example
Python :: are logN and (lognN) same 
Python :: string slice python 
Python :: visual studio code import library python 
Python :: how to split a string by space in python 
Python :: declaring list size python 
Python :: strip plot 
Python :: elif python 
Python :: split range python 
Python :: how to print 
Python :: remove n characters from string python 
Python :: os module in python 
Python :: install web3 on python 
Python :: python write float with 2 decimals 
Python :: python list operation 
Python :: python print every row of dataframe 
Python :: .corr python 
Python :: append element to list py 
Python :: stack python 
Python :: cross validation sklearn 
Python :: convert date to string in python 
Python :: duplicate remove 
Python :: session of flask 
Python :: handling exceptions 
Python :: django orm filter 
Python :: python print text 
Python :: python nested object to dict 
Python :: for _ in range() in python 
Python :: linked list python 
Python :: flask page 
Python :: python if in one line 
ADD CONTENT
Topic
Content
Source link
Name
3+4 =