Numpy에서는 모든 원소의 데이터 타입이 같아야 했지만 Pandas는 혼합된 데이터 타입을 인자로 이용할 수 있다.
데이터 생성 : Series()
series의 인자에는 시퀀스 데이터가 들어감. 주로 리스트 데이터를 이용
인자를 넣은 데이터에 순서를 표시하는 라벨이 자동으로 부여
s1 = pd.Series([10, 20, 30, 40, 50])
s1
---------------------------------------
# result
0 10
1 20
2 30
3 40
4 50
dtype: int64
index, values 분리해서 가져오기
s1.index
--------------------------
# result
RangeIndex(start=0, stop=5, step=1)
s1.values
---------------------
# result
array([10, 20, 30, 40, 50], dtype=int64)
없는 데이터 표시
데이터 없을때 Numpy 임포트 후 np.nan으로 표시 가능
nan은 결측치임 (missing values)
import numpy as np
s3 = pd.Series([np.nan, 10, 30]) # nan은 결측치 (missing values)
s3
-----------------------------------------------------------------
# result
0 NaN
1 10.0
2 30.0
dtype: float64
index 추가
pd.Series(seq_data, index = index_seq)
seq_data와 index_seq의 항목 개수 같아야 한다는 점 주의
index_date = ['2018-10-07', '2018-10-08']
s4 = pd.Series([200, 195], index = index_date)
s4
---------------------------------------------------
# result
2018-10-07 200
2018-10-08 195
dtype: int64
딕셔너리 형식으로 데이터와 index 입력
리스트 형태로 데이터 입력 시, 리스트 형태의 데이터와 index를 따로 입력했지만,
딕셔너리를 이용하면 데이터와 index를 함께 입력할 수 있음
pd.Series(dict_data)
data_dict = {
'국어' : 100,
'영어' : 95
}
s5 = pd.Series(data_dict)
s5
--------------------------------
# result
국어 100
영어 95
dtype: int64
날짜 자동 생성: date_range
pd.date_range(start = 시작날짜, end = 끝날짜, periods, fred='D')
periods는 날짜 데이터 생성 기간(=n개의 날짜), fred는 날짜 옵션
Time series / date functionality — pandas 2.1.4 documentation
Time series / date functionality pandas contains extensive capabilities and features for working with time series data for all domains. Using the NumPy datetime64 and timedelta64 dtypes, pandas has consolidated a large number of features from other Python
pandas.pydata.org
pd.date_range(start='2024/01/01', end='2024.01.07')
-------------------------------------------------------
# result
DatetimeIndex(['2024-01-01', '2024-01-02', '2024-01-03', '2024-01-04',
'2024-01-05', '2024-01-06', '2024-01-07'],
dtype='datetime64[ns]', freq='D')
pd.date_range(start='2024/01/01', periods = 7)
------------------------------------------------
# result
DatetimeIndex(['2024-01-01', '2024-01-02', '2024-01-03', '2024-01-04',
'2024-01-05', '2024-01-06', '2024-01-07'],
dtype='datetime64[ns]', freq='D')
pd.date_range(start='2024/01/01', periods = 4, freq = '2D')
----------------------------------------------------------------------------------------------------------
# result
DatetimeIndex(['2024-01-01', '2024-01-03', '2024-01-05', '2024-01-07'], dtype='datetime64[ns]', freq='2D')
pd.date_range(start='2024-01-01 08:00', periods = 4, freq = 'H')
-----------------------------------------------------------------
# result
DatetimeIndex(['2024-01-01 08:00:00', '2024-01-01 09:00:00',
'2024-01-01 10:00:00', '2024-01-01 11:00:00'],
dtype='datetime64[ns]', freq='H')
'Python > Python 기초문법' 카테고리의 다른 글
Python - Pandas DataFrame에서 일부 데이터 추출 (1) | 2024.01.04 |
---|---|
Python - Pandas DataFrame을 활용한 데이터 생성과 연산 (0) | 2024.01.04 |
Python - Numpy 조건문 (0) | 2024.01.04 |
Python - Numpy 배열의 연산 & 인덱싱과 슬라이싱 (0) | 2024.01.04 |
Python - Numpy 활용하여 로또 번호 생성하기 (1) | 2024.01.04 |