Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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 :: python get file path from in os.walk 
Python :: how to get a hyperlink in django 
Python :: correlation analysis of dataframe python 
Python :: pandas groupby aggregate 
Python :: python tkinter change color of main window 
Python :: get time python 
Python :: multinomial regression scikit learn 
Python :: where is tensorflow slim 
Python :: distplot with plotly 
Python :: select random value from list python 
Python :: python set and dictionary comprehensions 
Python :: how to check if text is in upper case in python 
Python :: how can i make a list of leftovers that are str to make them int in python 
Python :: curl in python 
Python :: length of pandas dataframe 
Python :: stop program python 
Python :: application/x-www-form-urlencoded python 
Python :: skip error python 
Python :: python convert string to lowercase 
Python :: isnumeric 
Python :: pandas select 2nd row 
Python :: split string and convert to int python 
Python :: python remove whitespace from start of string 
Python :: sort a list numbers in python 
Python :: data frame list value change to string 
Python :: train test split 
Python :: sort arr python 
Python :: python string to datetime object 
Python :: multiprocessing a for loop python 
Python :: how to capitalize the first letter in a list python 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =