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

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

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

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 :: finding the index of an element in a pandas df 
Python :: look through dict 
Python :: min of numpy array 
Python :: list adding to the begining python 
Python :: create models in django 
Python :: simple time in python 
Python :: how to get a dataframe column as a list 
Python :: rename column pandas 
Python :: flask get ip of user 
Python :: django connection cursor 
Python :: making a function wait in python 
Python :: Get List Into String 
Python :: python dataframe remove header 
Python :: pip install django rest framework 
Python :: python import multiple csv 
Python :: dataframe column data type 
Python :: stdout.write python 
Python :: reset django database 
Python :: how to use ggplot matplotlib 
Python :: decision tree classifier 
Python :: rename key in python dictionary 
Python :: free python script hosting 
Python :: case statement in querset django 
Python :: pytorch view -1 meaning 
Python :: Python make directories recursively 
Python :: networkx largest component 
Python :: calculating mean for pandas column 
Python :: python currency signs 
Python :: pandas replace colomns location 
Python :: how to search a file in windows 10 using python 
ADD CONTENT
Topic
Content
Source link
Name
5+6 =