[Python] 데이터프레임 values, columns, tolist() 사용하기

도갱도갱·2022년 1월 15일
0

Python

목록 보기
21/34

💡 values, columns를 이용해 데이터프레임의 값과 컬럼을 추출할 수 있다.
💡 tolist() 함수를 이용해서 값과 컬럼을 리스트로 만들수 있다.

데이터프레임 값 추출하기

✔ 반환 타입은 numpy array 이다.

print(type(df1.values))
print(df1.values)

[결과]
<class 'numpy.ndarray'>
[[ 1 2 3].
[ 11 22 33].
[111 222 333]]



데이터프레임 칼럼 추출하기

✔ columns, columns.values 반환 타입은 각 index, numpy array 이다.

# 인덱스 타입
print(type(df1.columns))
print(df1.columns)

# numpy arrary 타입
print(type(df1.columns.values))
print(df1.columns.values)

<class 'pandas.core.indexes.base.Index'>
Index(['col1', 'col2', 'col3'], dtype='object')
<class 'numpy.ndarray'>
['col1' 'col2' 'col3']



데이터프레임 값을 리스트로 만들기

✔ 반환 타입은 list 이다.

val_list = df1.values.tolist()
print(type(val_list))
print(val_list)

<class 'list'>
[[1, 2, 3], [11, 22, 33], [111, 222, 333]]



데이터프레임 특정 값만 리스트로 만들기

✔ 반환 타입은 list 이다.

col_val_list = df1['col1'].values.tolist()
print(type(col_val_list))
print(col_val_list)

<class 'list'>
[1, 11, 111]



데이터프레임 칼럼을 리스트로 만들기1

✔ index 타입에 tolist()를 사용하면 반환 타입은 list 이다.

col_list1 = df1.columns.tolist()
print(col_list1)
print(type(col_list1))

['col1', 'col2', 'col3']
<class 'list'>



데이터프레임 칼럼을 리스트로 만들기2

✔ numpy array 타입에 tolist()를 사용하면 반환 타입은 list 이다.

col_list2 = df1.columns.values.tolist()
print(col_list2)
print(type(col_list2))

['col1', 'col2', 'col3']
<class 'list'>



참고

0개의 댓글