파이썬/파이썬(python) 중급

[31-3 파이썬] pandas, to_dict(), orient, record

Olivia-BlackCherry 2022. 9. 24. 21:36

데이터 가공, 처리, 분석을 할 때

판다스 pandas를 많이 쓴다. 

 

판다스를 사용할 때 많이 쓰이는 유용한 기능 중에,

to_dict()에 대해 알아보도록 하자. 

 

먼저 데이터를 시각적으로 잘 보기 위해

형식을 DataFrame 데이터프레임으로 바꾼다.

그리고 DataFrame에 to_dict() 메소드를 적용한다.

 

to_dict는 to dictionary의 의미로, 

가지고 있는 데이터를 딕셔너리(키와 값을 쌍으로 하는) 형태로 바꾼다는 뜻이다. 

 

그런데 to dict()로 바꿨을 때

column, index, values를 나열하는 방식이 여러가지 인데,

이 모든 것은 

orient 파라미터로 결정된다.

 

 

french_words.csv

French,English
partie,part
histoire,history
chercher,search
seulement,only
police,police
pensais,thought
aide,help
demande,request
genre,kind

 

to_dict의 orient 파라미터

1) dict

Dataframe.to_dict(oreint="dict")

dict 형태로 출력

{column ->{index =>value}}

import pandas
data = pandas.read_csv("data/french_words.csv")
df = pandas.DataFrame(data)
dict_df = df.to_dict()
print(dict_df)

{'French': {0'partie', 1: 'histoire', 2: 'chercher', 3: 'seulement', 4: 'police', 5: 'pensais', 6: 'aide', 7: 'demande', 8: 'genre'}, 

'English': {0'part', 1: 'history', 2: 'search', 3: 'only', 4: 'police', 5: 'thought', 6: 'help', 7: 'request', 8: 'kind'}}

 

 

2) list

Dataframe.to_dict(orienct="list")

dict 형태로 출력

{column->[values]}

dict_df = df.to_dict(orient="list")

{'French': ['partie', 'histoire', 'chercher', 'seulement', 'police', 'pensais', 'aide', 'demande', 'genre'],

'English': ['part', 'history', 'search', 'only', 'police', 'thought', 'help', 'request', 'kind']}

 

 

3) split

DataFrame.to_dict(orient="split")

dict 형태로 출력

{'index'->[index], 'columns'->[columns], 'data'->[values]}

dict_df = df.to_dict(orient="split")

{'index': [0, 1, 2, 3, 4, 5, 6, 7, 8], 

'columns': ['French', 'English'],

'data': [['partie', 'part'], ['histoire', 'history'], ['chercher', 'search'], ['seulement', 'only'], ['police', 'police'], ['pensais', 'thought'], ['aide', 'help'], ['demande', 'request'], ['genre', 'kind']]}

 

 

4) records

DataFrame.to_dict(orient="records")

리스트 형태로 출력

[{column->value}, ..., {column->value}]

dict_df = df.to_dict(orient="records")

[{'French': 'partie', 'English': 'part'},

{'French'': 'histoire',  'English': 'history'},

{'French': 'chercher',  'English': 'search'},

{'French': 'seulement', 'English': 'only'},

{'French': 'police', 'English': 'police'},

{'French': 'pensais', 'English': 'thought'},

{'French': 'aide', 'English': 'help'},

{'French': 'demande', 'English': 'request'},

{'French': 'genre', 'English': 'kind'}]

 

 

5) index

DataFrame.to_dict(orient="index")

dict 형태로 출력

{index->{column->value}}

dict_df = df.to_dict(orient="index")

{0: {'French': 'partie', 'English': 'part'},

1: {'French': 'histoire', 'English': 'history'},

2: {'French': 'chercher', 'English': 'search'},

3: {'French': 'seulement', 'English': 'only'},

4: {'French': 'police', 'English': 'police'},

5: {'French': 'pensais', 'English': 'thought'},

6: {'French': 'aide', 'English': 'help'},

7: {'French': 'demande', 'English': 'request'},

8: {'French': 'genre', 'English': 'kind'}}