Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Convert list of dictionaries to a pandas DataFrame

import pandas as pd

your_list = [{'points': 50, 'time': '5:00', 'year': 2010}, 
 {'points': 25, 'time': '6:00', 'month': "february"}, 
 {'points':90, 'time': '9:00', 'month': 'january'}, 
 {'points_h1':20, 'month': 'june'}]

df = pd.DataFrame(your_list)
Comment

dataframe to list of dicts

df = pd.DataFrame({'Name': ['John', 'Sara','Peter','Cecilia'],
                   'Age': [38, 47,63,28],
                  'City':['Boston', 'Charlotte','London','Memphis']})

datadict = df.to_dict('records')
Comment

list of dict to dataframe

Supposing d is your list of dicts, simply:
import pandas as pd 

df = pd.DataFrame(d)
Comment

python list of dict to dataframe

# Supposing d is your list of dicts, simply
df = pd.DataFrame(d)
# Note: this does not work with nested data
Comment

pandas df to list of dictionaries

In [2]: df.to_dict('records')
Out[2]:
[{'customer': 1L, 'item1': 'apple', 'item2': 'milk', 'item3': 'tomato'},
 {'customer': 2L, 'item1': 'water', 'item2': 'orange', 'item3': 'potato'},
 {'customer': 3L, 'item1': 'juice', 'item2': 'mango', 'item3': 'chips'}]
Comment

list of dict to df

df = pd.DataFrame(d)  #here d is list of dict 
Comment

Converting Dataframe from list Using a list in the dictionary

# import pandas as pd 

import pandas as pd 
# list of name, degree, score 
n = ["apple", "grape", "orange", "mango"] 
col = ["red", "green", "orange", "yellow"] 
val = [44, 33, 22, 11] 
# dictionary of lists 
dict = {'fruit': n, 'color': col, 'value': val}  
df = pd.DataFrame(dict) 
print(df)
Comment

pandas to list of dicts

In [20]: timeit df.T.to_dict().values()
1000 loops, best of 3: 395 µs per loop

In [21]: timeit df.to_dict('records')
10000 loops, best of 3: 53 µs per loop
Comment

Convert list of dictionaries to a pandas DataFrame

np.random.seed(0)
data = pd.DataFrame(
    np.random.choice(10, (3, 4)), columns=list('ABCD')).to_dict('r')

print(data)
[{'A': 5, 'B': 0, 'C': 3, 'D': 3},
 {'A': 7, 'B': 9, 'C': 3, 'D': 5},
 {'A': 2, 'B': 4, 'C': 7, 'D': 6}]
Comment

PREVIOUS NEXT
Code Example
Python :: python read file xlsx and return a list 
Python :: restrict ticks to integers matplotlib 
Python :: if else python 
Python :: python turtle delay 
Python :: executing curl commands in python 
Python :: open file dialog on button click pyqt5 
Python :: write image out opencv 
Python :: python is space 
Python :: django filter by category 
Python :: create virtual env pyhton3 
Python :: python check if file is writable 
Python :: box plot python 
Python :: Modify a Python interpreter 
Python :: dataframe of one row 
Python :: Python Datetime Get year, month, hour, minute, and timestamp 
Python :: two groupby pandas 
Python :: tqdm description 
Python :: remove all elements from list python by value 
Python :: how to use modulo in python 
Python :: python split 
Python :: pyton recognize any datetime format 
Python :: waiting in python. time , sleep 
Python :: assert integer python 
Python :: pytorch cuda tensor in module 
Python :: python bytes to string 
Python :: plt delete space before axis 
Python :: how to change int to string in python 
Python :: python multithreading 
Python :: mean python 
Python :: matrix multiplication nupy 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =