본문 바로가기
데이터분석/Pandas

[Python] Pandas 02 _pandas_1D_Series (2)

by nemonemonemo 2025. 8. 19.

pandas에서 FM적으로 값을 접근하는 방법들

  • 값에 대한 접근을 바라보는 관점 : 1개 값 접근 vs 여러개 값 접근
    • 1개 값 접근
      • at : 내가 만든 인덱스로 1개 값 접근
      • iat : 태생적인 정수 인덱스로 1개 값 접근
    • 여러개 값을 접근
      • loc : 내가 만든 인덱스로 여러개 접근
      • iloc : 태생적인 정수인덱스로 여러개 접근
    • ⇒ 이 부분에 대해서 최대한 타이트하게 구분해서 사용하는 것을 추천 속도 / 효율성 떄문임!!!! ( 많은 데이터처리할 때!!! )
import pandas as pd

stock_price_Series_index = pd.Series(
    # 정보 : 진짜 리얼한 데이터,,,
    #      : 나만의 접근 할 수 있는 인덱스 정보
    data = [70000, 73000, 69000, 72000, 71000],
    index = ["2025-08-01", "2025-08-02", "2025-08-03",
            "2025-08-04", "2025-08-5"]
)
stock_price_Series_index

stock_price_Series_index[0] # --> 값에 대한 접근이 가능하지만,,AM
#/tmp/ipython-input-2592654350.py:1: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`
#  stock_price_Series_index[0]
#np.int64(70000)

# 태생적인 정수 인덱스 & 0번째 --> 1개 값에 대한 접근 FM
stock_price_Series_index.iat[0]
#np.int64(70000)

stock_price_Series_index.iloc[0]
#np.int64(70000)
#===> 미묘하게 at/loc 계열이 속도가 좀 차이 남
#     최대한 타이트하게 1개냐 여러개냐 구별해서 사용하는 것 추천
# [0] >>>> iloc[0] > iat[0]

# 2025년 8월 2일 가격 데이터 보자 
stock_price_Series_index.at["2025-08-02"]
#np.int64(73000)

stock_price_Series_index.iat["2025-08-02"] #오류남 
#ValueError: iAt based indexing can only have integer indexers

#0번째 데이터부터 4번째 데이터의 날까지의 주가 데이터를 접근
# --> 여러개 + 위치기반 (정수) : iloc
stock_price_Series_index[:4] #가능하긴 한데 표준적인 방법은 아님
#                0
#2025-08-01	70000
#2025-08-02	73000
#2025-08-03	69000
#2025-08-04	72000
#
#dtype: int64

stock_price_Series_index.iloc[:4]#이게 표준 
##                0
#2025-08-01	70000
#2025-08-02	73000
#2025-08-03	69000
#2025-08-04	72000
#
#dtype: int64

#2025년 8월 2일부터 8월 4일까지 데이터 보여주세요 

stock_price_Series_index.loc["2025-08-02":"2025-08-04"]
#               0
#2025-08-02	73000
#2025-08-03	69000
#2025-08-04	72000
#
#dtype: int64

#얘도 numpy를 업그레이드를 한 것 -> 연산자 내포하고 있음

stock_price_Series_index
#	             0
#2025-08-01	70000
#2025-08-02	73000
#2025-08-03	69000
#2025-08-04	72000
#2025-08-5	71000
#
#dtype: int64

stock_price_Series_index.sum()
##np.int64(355000)

sum(stock_price_Series_index)
##355000

stock_price_Series_index.count()
##np.int64(5)

len(stock_price_Series_index)
##5

stock_price_Series_index.max()
##73000

max(stock_price_Series_index)
##73000

1차원에 대한 것

⇒ 2차원에 대한 DataFrame

  • 지금까지 동작이 동일함/ 단, 차원에 대한 부분만 2차원으로 확장
가로에 대한 접근  세로에 대한 접근
내가 만든 index 내가 만든 columns

 

'데이터분석 > Pandas' 카테고리의 다른 글

[Python] Pandas 06 _ kobis_api_xml  (0) 2025.08.20
[Python] pandas 05_kobis_api_json  (0) 2025.08.19
[Python] Pandas 04 _ json  (0) 2025.08.19
[Python] Pandas 03 _ pandas_2D_DataFrame  (3) 2025.08.19
[Python] Pandas 02 _pandas_1D_Series (1)  (4) 2025.08.18