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

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 Tkinter Frame Widget 
Python :: how to change the colour of axes in matplotlin 
Python :: iterate through list python with index 
Python :: pthon return value with highest occurences 
Python :: stack error: command failed: import sys; print "%s.%s.%s" % sys.version_info[:3]; 
Python :: Check if file already existing 
Python :: indexes meta django 
Python :: find nan values in pandas 
Python :: python remove .0 
Python :: how to remove a letter from a string python 
Python :: how to remove element from nested list in python 
Python :: converting tuple into string 
Python :: turtle.write("Sun", move=False, align="left", font=("Arial", 8, "normal")) 
Python :: python cls command line 
Python :: if condition in print statement python 
Python :: concact geodataframe python 
Python :: pandas selection row/columns 
Python :: install ansible with pip 
Python :: python scheduler 
Python :: max between two numbers python 
Python :: find index of sublist in list python 
Python :: python garbaze collection 
Python :: requests save file python 
Python :: django login 
Python :: best ide for python 
Python :: datetime.timedelta format to string python 
Python :: python tkinter dynamic toggle button 
Python :: how to write a comment in python 
Python :: python merge strings 
Python :: pafy doc 
ADD CONTENT
Topic
Content
Source link
Name
8+4 =