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

convert dict to dataframe

#Lazy way to convert json dict to df

pd.DataFrame.from_dict(data, orient='index').T
Comment

convert pandas dataframe/ table to python dictionary

df.to_dict(orient='index')
Comment

dataframe from dict

pd.DataFrame.from_dict(data)
Comment

dataframe to dictionary

>>> df.to_dict('records')
[{'col1': 1, 'col2': 0.5}, {'col1': 2, 'col2': 0.75}]
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

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

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

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 :: version of scikit learn 
Python :: python capture in regex 
Python :: python get index of item in 2d list 
Python :: how to find if a value is even or odd in python 
Python :: python for get index and value 
Python :: How do you sum consecutive numbers in Python? 
Python :: pydrive list folders 
Python :: python object to json file 
Python :: python clipboard to image 
Python :: how to maker loops coun t in second in pytho 
Python :: using regex validators in django models 
Python :: django foreign key field on delete do nothing 
Python :: f string curency format 
Python :: install postgres for python mac 
Python :: cv2 show image 
Python :: calculate euclidian distance python 
Python :: python day from datetime 
Python :: resize image array python 
Python :: create pyspark session with hive support 
Python :: convert tuple to array python 
Python :: django admin slug auto populate 
Python :: How to develop a TCP echo server, in Python? 
Python :: python string list to float 
Python :: python pygame key input 
Python :: how to square each term of numpy array python 
Python :: Renaming row value in pandas 
Python :: python random dictionary 
Python :: python loop every month datetime 
Python :: python - remove repeted columns in a df 
Python :: print(np.round(df.isnull().sum() / len(df), 2)) 
ADD CONTENT
Topic
Content
Source link
Name
9+3 =