pandas to_dict

Published by onesixx on

df.to_dict(‘dict’, list,
‘split , ‘index’, ‘series’, ‘records’)

txt="John,20,student\
Jenny,30,developer\
Nate,30,teacher\
Julia,40,dentist\
Brian,45,manager\
Chris,25,intern\
"
# name,age,job
# John,20,student
# Jenny,30,developer
# Nate,30,teacher
# Julia,40,dentist
# Brian,45,manager
# Chris,25,intern

import pandas as pd
from io import StringIO

df = pd.read_csv(StringIO(txt), header=None)
df.columns=['name','age','job']
[colNm for colNm in df]
# ['name', 'age', 'job']

import pandas as pd

data ='https://raw.githubusercontent.com/plotly/datasets/master/gapminder2007.csv' 
df = pd.read_csv(data)

df.shape  # rows × columns  (142, 5)  
df.shape[0]      # rows 
len(df)   
len(df.index)    

df.shape[1]
len(df.columns)  # columms

df.size     # cell 갯수   710

df.count()  # Null아닌 열별 행갯수 

df.to_dict()            # type은 dict  (row번호)
len(df.to_dict())       # 5

df.to_dict('list')      # type은 dict
len(df.to_dict('list')) # 5 

df.to_dict('split')     # type은 dict   
len(df.to_dict('split')) # 3,  index, columns, data

df.to_dict('index')     # type은 dict   
len(df.to_dict('index')) # 142

df.to_dict('records')   # type은 list
len(df.to_dict('records'))  # 142
dff =df[0:2]
#   country\t    pop\t        continent\tlifeExp\t  gdpPercap
# 0\tAfghanistan\t31889923.0\tAsia       \t43.828\t 974.580338
# 1\tAlbania\t    3600523.0\tEurope\t    76.423\t5937.029526

dff.shape  
#(2,5)

dff.size 
# 10 
dff.count()  # Null아닌 열별 행갯수 
# country      2
# pop          2
# continent    2
# lifeExp      2
# gdpPercap    2
# dtype: int64

dff.to_dict()
# {'country':   {0: 'Afghanistan', 1: 'Albania'},
#  'pop':       {0: 31889923.0,    1: 3600523.0},
#  'continent': {0: 'Asia',        1: 'Europe'},
#  'lifeExp':   {0: 43.828,        1: 76.423},
#  'gdpPercap': {0: 974.5803384,   1: 5937.029525999999}}

dff.to_dict('list')     
# {'country':   ['Afghanistan', 'Albania'],
#  'pop':       [31889923.0, 3600523.0],
#  'continent': ['Asia', 'Europe'],
#  'lifeExp':   [43.828,  76.423],
#  'gdpPercap': [974.5803384, 5937.029525999999]}

dff.to_dict('records')   
# [{'country':   'Afghanistan',
#   'pop':       31889923.0,
#   'continent': 'Asia',
#   'lifeExp':   43.828,
#   'gdpPercap': 974.5803384},
#  {'country':   'Albania',
#   'pop':       3600523.0,
#   'continent': 'Europe',
#   'lifeExp':   76.423,
#   'gdpPercap': 5937.029525999999}]

Categories: pandas

onesixx

Blog Owner

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x