Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pandas dataframe from dict

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

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

pandas dataframe from dict

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

pandas dataframe from dict

>>> 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

pandas dataframe from dict

>>> pd.DataFrame.from_dict(data, orient='index',
...                        columns=['A', 'B', 'C', 'D'])
       A  B  C  D
row_1  3  2  1  0
row_2  a  b  c  d
Comment

convert dict to dataframe

#Lazy way to convert json dict to df

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

pandas read 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

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

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

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

pandas dataframe from dict

# import pandas library
import pandas as pd
  
# dictionary
details = {
    'Ankit' : 22,
    'Golu' : 21,
    'hacker' : 23
    }
  
# creating a Dataframe object from a list 
# of tuples of key, value pair
df = pd.DataFrame(list(details.items()))
  
df
Comment

PREVIOUS NEXT
Code Example
Python :: python to linux executable 
Python :: structure ternaire python 
Python :: align a text python 
Python :: how to serach for multiple attributes in xpath selenium python 
Python :: drf serializer general validate method 
Python :: group by pandas 
Python :: python seaborn violin plot 
Python :: how to use coordinates in python 
Python :: pandas if value present in df index 
Python :: help() function in python 
Python :: data type array 
Python :: tensorflow evaluation metrics 
Python :: django class based views 
Python :: cropping image google colab 
Python :: scatter density plot seaborn 
Python :: python keyboard input 
Python :: python internship 
Python :: how to merge dictionaries in python 
Python :: np.array([(1,2),(3,4)],dtype 
Python :: get request in django 
Python :: fill zeros left python 
Python :: django on delete set default 
Python :: django loginview 
Python :: bubblesort python 
Python :: concatenating strings 
Python :: Access the Response Methods and Attributes in python Show HTTP header 
Python :: merge dataframe using pandas 
Python :: change python from 3.8 to 3.7 
Python :: pd df iloc 
Python :: python new line 
ADD CONTENT
Topic
Content
Source link
Name
1+7 =