Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

convert a dictionary into dataframe python

import pandas as pd
data = {'col_1': [3, 2, 1, 0], 'col_2': ['a', 'b', 'c', 'd']}
pd.DataFrame.from_dict(data, orient='index').T
Comment

build dataframe from dictionary

data = {'col_1': [3, 2, 1, 0], 'col_2': ['a', 'b', 'c', 'd']}
>>> pd.DataFrame.from_dict(data)
   col_1 col_2
0      3     a
1      2     b
2      1     c
3      0     d
Comment

create pandas dataframe from dictionary

In [11]: pd.DataFrame(d.items())  # or list(d.items()) in python 3
Out[11]:
             0    1
0   2012-07-02  392
1   2012-07-06  392
2   2012-06-29  391
3   2012-06-28  391
...

In [12]: pd.DataFrame(d.items(), columns=['Date', 'DateValue'])
Out[12]:
          Date  DateValue
0   2012-07-02        392
1   2012-07-06        392
2   2012-06-29        391
Comment

create a dataframe from dict

create a dataframe from dict (function)

import pandas as pd
def create_df():
    data = {'col_1': [3, 2, 1, 0], 'col_2': ['a', 'b', 'c', 'd']}
    return pd.DataFrame.from_dict(data)
create_df()
Comment

create a dictionary from dataframe

input->
		a      b
0     red  0.500
1  yellow  0.250
2    blue  0.125

dict(df.values)

output -> {'red': '0.500', 'yellow': '0.250', 'blue': '0.125'}
Comment

PREVIOUS NEXT
Code Example
Python :: finding anagrams in python 
Python :: how to open an application in python 
Python :: dataset.shape 
Python :: pandas merge validate 
Python :: default arguments 
Python :: empty python file 
Python :: string -1 python 
Python :: concat series to dataframe 
Python :: how to get user input in python 
Python :: if else condition python 
Python :: remove outlier using IQR 
Python :: sort list of list of dictionaries python 
Python :: def tkinter 
Python :: panda 
Python :: bounding box in pyplot 
Python :: graph outlier detection 
Python :: rotate 2 dimensional list python 
Python :: how to add virtual environment in vscode 
Python :: prime numbers 1 to 100 
Python :: how to read frame width of video in cv2 
Python :: python buffer 
Python :: python get all numbers between two numbers 
Python :: function python 
Python :: pine script to python 
Python :: dictionary lookup python 
Python :: python matrix determinant without numpy 
Python :: check if object is list python 
Python :: python function to do comparison between two numbers 
Python :: python update dict if key not exist 
Python :: python sort dictionary case insensitive 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =