본문 바로가기

Python_Intermediate/Json

JSON - Data Frame Conversion(Pandas 분석)

1. Sample JSON Site : https://api.androidhive.info/contacts/


2. Import Module

import requests
import json
from pandas import DataFrame
from print_df import print_df


3. Sample Code

import requests
import json
from pandas import DataFrame
from print_df import print_df


url = 'https://api.androidhive.info/contacts'

response = requests.get(url)

if response.status_code != 200:
print("[%d Error] %s" % (response.status_code, response.reason))
quit()

response.encoding = 'UTF-8'

result = json.loads(response.text)

df = DataFrame(result['contacts'])

name = []

for i in result['contacts']:
name.append(i['name'])

name_dict = {}
for i, v in enumerate(name):
name_dict[i] = v

df.rename(index=name_dict, inplace=True)
df.drop('name', axis=1, inplace=True)

print_df(df)

<class 'pandas.core.frame.DataFrame'>

(13, 5)

+-------------------+------------------------------------+-----------------------------+--------+-------+--------------------------------------------------------------------------+

|                   |              address               |            email            | gender |   id  |                                  phone                                   |

+-------------------+------------------------------------+-----------------------------+--------+-------+--------------------------------------------------------------------------+

|    Ravi Tamada    | xx-xx-xxxx,x - street, x - country |        ravi@gmail.com       |  male  |  c200 | {'mobile': '+91 0000000000', 'home': '00 000000', 'office': '00 000000'} |

|    Johnny Depp    | xx-xx-xxxx,x - street, x - country |    johnny_depp@gmail.com    |  male  |  c201 | {'mobile': '+91 0000000000', 'home': '00 000000', 'office': '00 000000'} |

| Leonardo Dicaprio | xx-xx-xxxx,x - street, x - country | leonardo_dicaprio@gmail.com |  male  |  c202 | {'mobile': '+91 0000000000', 'home': '00 000000', 'office': '00 000000'} |

|     John Wayne    | xx-xx-xxxx,x - street, x - country |     john_wayne@gmail.com    |  male  |  c203 | {'mobile': '+91 0000000000', 'home': '00 000000', 'office': '00 000000'} |

|   Angelina Jolie  | xx-xx-xxxx,x - street, x - country |   angelina_jolie@gmail.com  | female |  c204 | {'mobile': '+91 0000000000', 'home': '00 000000', 'office': '00 000000'} |

|        Dido       | xx-xx-xxxx,x - street, x - country |        dido@gmail.com       | female |  c205 | {'mobile': '+91 0000000000', 'home': '00 000000', 'office': '00 000000'} |

|       Adele       | xx-xx-xxxx,x - street, x - country |       adele@gmail.com       | female |  c206 | {'mobile': '+91 0000000000', 'home': '00 000000', 'office': '00 000000'} |

|    Hugh Jackman   | xx-xx-xxxx,x - street, x - country |    hugh_jackman@gmail.com   |  male  |  c207 | {'mobile': '+91 0000000000', 'home': '00 000000', 'office': '00 000000'} |

|     Will Smith    | xx-xx-xxxx,x - street, x - country |     will_smith@gmail.com    |  male  |  c208 | {'mobile': '+91 0000000000', 'home': '00 000000', 'office': '00 000000'} |

|   Clint Eastwood  | xx-xx-xxxx,x - street, x - country |   clint_eastwood@gmail.com  |  male  |  c209 | {'mobile': '+91 0000000000', 'home': '00 000000', 'office': '00 000000'} |

|    Barack Obama   | xx-xx-xxxx,x - street, x - country |    barack_obama@gmail.com   |  male  | c2010 | {'mobile': '+91 0000000000', 'home': '00 000000', 'office': '00 000000'} |

|    Kate Winslet   | xx-xx-xxxx,x - street, x - country |    kate_winslet@gmail.com   | female | c2011 | {'mobile': '+91 0000000000', 'home': '00 000000', 'office': '00 000000'} |

|       Eminem      | xx-xx-xxxx,x - street, x - country |       eminem@gmail.com      |  male  | c2012 | {'mobile': '+91 0000000000', 'home': '00 000000', 'office': '00 000000'} |

+-------------------+------------------------------------+-----------------------------+--------+-------+--------------------------------------------------------------------------+




Process finished with exit code 0


4. Sample Code 풀이

- 데이터 프레임으로 변환 작업

df = DataFrame(result['contacts'])


- name을 인덱스 대신 사용하기 위해 리스트 형식으로 추출

name = []

for i in result['contacts']:
name.append(i['name'])


- name 딕셔너리 생성

name_dict = {}
for i, v in enumerate(name):
name_dict[i] = v


- Data Frame의 인덱스 변경 및 name 컬럼 삭제

df.rename(index=name_dict, inplace=True)
df.drop('name', axis=1, inplace=True)