Programming Language/Python2022. 3. 12. 15:54
728x90

- 데이터 분석 모듈로 시리즈(Series)와 데이터프레임(DataFrame) 데이터구조를 제공한다.
- 시리즈가 1차원 구조면 데이터프레임은 2차원의 데이터 구조다.

 

  • 판다스 데이터 구조
=import pandas as pd

# 시리즈
sr = pd.Series([400, 300, 800])
# 첫번째 숫자는 인덱스
print(sr)
# 0    400
# 1    300
# 2    800
# dtype: int64

sr = pd.Series([400, 300, 800], index=['두산퓨얼셀', 'OCI', '대한항공'])
print(type(sr))
# <class 'pandas.core.series.Series'>

# 인덱스를 지정했을 때
print(sr)
# 두산퓨얼셀     400
# OCI    300
# 대한항공     800
# dtype: int64

print(type(sr['두산퓨얼셀']))
# <class 'numpy.int64'>
print(sr['두산퓨얼셀'])
# 400
print(sr[1])
# 300

# 데이터프레임
df = pd.DataFrame({'가격':[40000, 110000, 29000], 'PER': [0.5, 1.2, 0.2], 'ROA': [1.01, 3.1, 0.97]}, index=['두산퓨얼셀', 'OCI', '대한항공'])
print(type(df))
# <class 'pandas.core.frame.DataFrame'>
print(df)
# <class 'pandas.core.frame.DataFrame'>
#         가격  PER   ROA
# 두산퓨얼셀    40000  0.5  1.01
# OCI   110000  1.2  3.10
# 대한항공   29000  0.2  0.97

print(type(df['가격']))
# <class 'pandas.core.series.Series'>

# 시리즈데이터 가져오기
print(df['가격'])
# 두산퓨얼셀      40000
# OCI     110000
# 대한항공     29000
# Name: 가격, dtype: int64
print(df['가격']['OCI'])
# 110000

# 행데이터 가져오기
print(df.loc['두산퓨얼셀'])
# 가격     40000.00
# PER      0.50
# ROA      1.01
# Name: 두산퓨얼셀, dtype: float64

# 행데이터도, 시리즈 타입이네요.
print(type(df.loc['두산퓨얼셀']))
# <class 'pandas.core.series.Series'>
728x90
Posted by 하루y