[python] DataFrame 생성하기/ 넘파이 ndarray, 리스트, 딕셔너리로 변환하기

2022. 3. 24. 19:35·python
728x90
반응형

1. 넘파이 ndarray, 리스트, 딕셔너리를 => DataFrame으로 변환하기

 

1.1  리스트 list= > dataframe으로 변환

df_list2=pd.DataFrame([[1,2,3],[11,12,13]],columns=['col1','col2','col3'])
df_list2

 

1.2  ndarray = > dataframe으로 변환

df_array2=pd.DataFrame(np.array([[1,2,3],[11,12,13]]), columns=col_name2)
df_array2

 

1.3  딕셔너리 = > dataframe으로 변환

# key => 문자열 칼럼명, value=>  칼럼 데이터로 매핑

dict={'col1':[1,11], 'col2': [2,22], 'col3': [3,33]}

df_dict=pd.DataFrame(dict)
df_dict

 

2. DataFrame을 => 넘파이 ndarray, 리스트, 딕셔너리로 변환하기

2.1  DataFrame= > ndarray 으로 변환

array3=df_dict.values #dataframe => ndarray
array3
array([[ 1,  2,  3],
       [11, 22, 33]], dtype=int64)

#ndarray => list

 

2.1  ndarray = >list 로 변환 ( tolist() )

list3= df_dict.values.tolist() #ndarray => list
print(list3)
[[1, 2, 3], [11, 22, 33]]

 

2.3  DataFrame= >dict 로 변환

1) column 기준으로 딕셔너리 만들기 {column : {index: value}}

dict3= df_dict.to_dict() #dataframe => dict
dict3
{'col1': {0: 1, 1: 11}, 'col2': {0: 2, 1: 22}, 'col3': {0: 3, 1: 33}}

 

2) column 기준으로 리스트로 반환 {column : list}

dict3= df_dict.to_dict('list') #dataframe => dict
dict3
{'col1': [1, 11], 'col2': [2, 22], 'col3': [3, 33]}

 

3) column 기준으로 series로 반환 {column : series}

dict3= df_dict.to_dict('series') #dataframe => dict
dict3
{'col1': 0     1
         1    11
 Name: col1, dtype: int64,
 'col2': 0     2
         1    22
 Name: col2, dtype: int64,
 'col3': 0     3
         1    33
 Name: col3, dtype: int64}

 

4) 각 row를 딕셔너리로 해서 리스트로 반환 [{column1 : value1, column2 : value2}]

dict3= df_dict.to_dict('records') #dataframe => dict
dict3
[{'col1': 1, 'col2': 2, 'col3': 3}, {'col1': 11, 'col2': 22, 'col3': 33}]

 

5) 각 row를 딕셔너리로 해서 딕셔너리로 반환 {index : {column1 : value1, column2 : value2}}

(주식데이터를 다루면 index가 보통 시간으로 나타내는데 시간에 따라 딕셔너리를 할 수 있어서 유용함)

dict3= df_dict.to_dict('index') #dataframe => dict
dict3
{0: {'col1': 1, 'col2': 2, 'col3': 3}, 1: {'col1': 11, 'col2': 22, 'col3': 33}}

 

 

 

728x90
반응형

'python' 카테고리의 다른 글

[python] bar / barh 그래프  (0) 2022.05.19
[python] axis=0 axis=1  (0) 2022.04.18
[python] plot / bar / scatter 그래프  (0) 2022.03.24
[python] figure & axes  (0) 2022.03.24
[python] groupby절  (0) 2022.03.23
'python' 카테고리의 다른 글
  • [python] bar / barh 그래프
  • [python] axis=0 axis=1
  • [python] plot / bar / scatter 그래프
  • [python] figure & axes
ISFP의 블로그
ISFP의 블로그
이건 첫 번째 레슨, 업무에서 마주친 문제 해결 경험 공유하기 이건 두 번째 레슨, 개인적으로 공부한 데이터/AI 지식을 기록하기 이건 세 번째 레슨, 다른 사람과 비교하지 말고 오직 어제의 나와 비교하기
  • ISFP의 블로그
    resultofeffort
    ISFP의 블로그
  • 전체
    오늘
    어제
    • 분류 전체보기 (117)
      • python (25)
      • pythonML (27)
      • Linux (0)
      • 오류Error (8)
      • information (7)
      • Deep learning (5)
      • pytorch (29)
      • 코딩테스트 (4)
      • 밑바닥 DL (4)
      • 논문 리뷰 (3)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    자연어처리
    cnn
    Pandas
    Ai
    pytorch
    인공지능
    machinelearning
    데이터분석
    머신러닝
    토큰화
    konlpy
    Deep Learning
    nlp
    텍스트전처리
    티스토리챌린지
    오블완
    분류
    딥러닝
    deeplearning
    Python
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.5
ISFP의 블로그
[python] DataFrame 생성하기/ 넘파이 ndarray, 리스트, 딕셔너리로 변환하기
상단으로

티스토리툴바