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

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

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 :: python extract list from string 
Python :: pandas remove outliers 
Python :: how to stop a program after 1 second in python 
Python :: map example in python 
Python :: python create array 
Python :: planets with python coding 
Python :: python pop 
Python :: python nominatim get latitude from address 
Python :: alpha vantage import 
Python :: adding text cv2 
Python :: python imaplib send email 
Python :: python dictionary append value if key exists 
Python :: python ufeff 
Python :: pandas eliminar filas de un dataframe con una condicion 
Python :: how to call a random function in python 
Python :: __delattr__ python 
Python :: python delete from dictionary 
Python :: pandas drop missing values for any column 
Python :: pandas cartesian product 
Python :: open word document python 
Python :: add two column values of a datframe into one 
Python :: How to get the first and last values from the dataframe column using a function 
Python :: face detection code 
Python :: next iteration python 
Python :: tkinter pack grid and place 
Python :: timer in python 
Python :: integral python 
Python :: python dictonary set default 
Python :: model.fit(X_train, Y_train, batch_size=80, epochs=2, validation_split=0.1) 
Python :: create the dataframe column based on condition 
ADD CONTENT
Topic
Content
Source link
Name
7+1 =