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

turn df to dict

dict(df.values)
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

pandas to dictionary

df.to_dict('records')
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

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

PREVIOUS NEXT
Code Example
Python :: How to perform Bubble sort in Python? 
Python :: python dict key delete 
Python :: how to make a latency command in discord py 
Python :: read binary image python 
Python :: django models.py convert DateTimeField to DateField 
Python :: how to check an element in a list in python 
Python :: urllib.request.urlretrieve 
Python :: operator precedence in python 
Python :: numpy is not nan 
Python :: turn false true column into 0 1 pandas 
Python :: sum of 2 numbers in python 
Python :: pandas iterate rows 
Python :: show equation using geom_smooth 
Python :: fetch email from gmail using python site:stackoverflow.com 
Python :: dataframe standardise 
Python :: python pip jupyter notebook install 
Python :: plot second y axis matplotlib 
Python :: django include all columns admin show 
Python :: how to check if there are duplicates in a list python 
Python :: numpy linspace 
Python :: python create file if doesnt exist 
Python :: print statement in python 
Python :: check all values in dictionary python 
Python :: pandas apply function to each row lambda 
Python :: how to take input for list in python 
Python :: Converting Hex to RGB value in Python 
Python :: python sort list 
Python :: OneHotEncoder() 
Python :: python timedelta to seconds 
Python :: box plot seaborn python 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =