1. 시리즈 만들기¶
In [1]:
import pandas as pd
In [3]:
s = pd.Series(['apple', 33]) # 인덱스는 보통 0부터 시작
print(s)
0 apple
1 33
dtype: object
In [5]:
list_data = ['2023-04-28', 3.14, 'encore', 100, True]
s = pd.Series(list_data)
print(s)
print('\n')
print(type(s))
0 2023-04-28
1 3.14
2 encore
3 100
4 True
dtype: object
<class 'pandas.core.series.Series'>
In [7]:
# 인덱스 배열은 변수 idx에 저장, 데이터 값 배열은 변수 val에 저장
idx = s.index
val = s.values
print(idx)
print('\n')
print(val)
RangeIndex(start=0, stop=5, step=1)
['2023-04-28' 3.14 'encore' 100 True]
In [8]:
# 딕셔너리를 Series() 함수에 전달하면 시리즈 객체로 변환
dict_data = {'a':1, 'b':2, 'c':3}
s = pd.Series(dict_data)
print(s)
print(type(s))
a 1
b 2
c 3
dtype: int64
<class 'pandas.core.series.Series'>
In [9]:
s = pd.Series(['Jane', 'student'], index=['Person', 'Job'])
print(s)
Person Jane
Job student
dtype: object
3. 딕셔너리로 데이터 프레임 생성¶
- 파이썬의 기본 자료 구조로 데이터 프레임 생성이 가능
- 아래의 예제는 딕셔너리로 데이터 프레임을 생성하는 예제
- 딕셔너리를 DataFrame 클래스에 전달해야 함
- 데이터 프레임의 컬럼은 모두 시리즈임
- 아래의 예제는 5개의 시리즈로 구성된 데이터 프레임
In [13]:
scientists = pd.DataFrame({
'Name': ['Rosaline Franklin', 'William Gosset'],
'Occupation': ['Chemist', 'Statistician'],
'Born': ['1920-07-25', '1876-06-13'],
'Died': ['1958-04-16', '1937-10-16'],
'Age': [37, 61],
})
scientists
Out[13]:
Name | Occupation | Born | Died | Age | |
---|---|---|---|---|---|
0 | Rosaline Franklin | Chemist | 1920-07-25 | 1958-04-16 | 37 |
1 | William Gosset | Statistician | 1876-06-13 | 1937-10-16 | 61 |
In [19]:
scientists = pd.DataFrame(
data = {'Occupation': ['Chemist', 'Statistician'],
'Born': ['1920-07-25', '1876-06-13'],
'Died': ['1958-04-16', '1937-10-16'],
'Age': [37, 61]},
index = ['Rosaline Franklin', 'William Gosset'],
columns = ['Occupation', 'Born', 'Age', 'Died']
)
scientists
Out[19]:
Occupation | Born | Age | Died | |
---|---|---|---|---|
Rosaline Franklin | Chemist | 1920-07-25 | 37 | 1958-04-16 |
William Gosset | Statistician | 1876-06-13 | 61 | 1937-10-16 |
In [24]:
from collections import OrderedDict
scientists = pd.DataFrame(OrderedDict([
('Name', ['Rosaline Franklin', 'William Gosset']),
('Occupation', ['Chemist', 'Statistician']),
('Born', ['1920-07-25', '1876-06-13']),
('Died', ['1958-04-16', '1937-10-16']),
('Age', [37, 61])
])
)
scientists
Out[24]:
Name | Occupation | Born | Died | Age | |
---|---|---|---|---|---|
0 | Rosaline Franklin | Chemist | 1920-07-25 | 1958-04-16 | 37 |
1 | William Gosset | Statistician | 1876-06-13 | 1937-10-16 | 61 |
데이터 프레임에서 시리즈 선택하기¶
In [25]:
scientists = pd.DataFrame(
data = {'Occupation': ['Chemist', 'Statistician'],
'Born': ['1920-07-25', '1876-06-13'],
'Died': ['1958-04-16', '1937-10-16'],
'Age': [37, 61]},
index = ['Rosaline Franklin', 'William Gosset'],
columns = ['Occupation', 'Born', 'Died', 'Age']
)
scientists
Out[25]:
Occupation | Born | Died | Age | |
---|---|---|---|---|
Rosaline Franklin | Chemist | 1920-07-25 | 1958-04-16 | 37 |
William Gosset | Statistician | 1876-06-13 | 1937-10-16 | 61 |
In [26]:
first_row = scientists.loc['William Gosset']
print(type(first_row))
<class 'pandas.core.series.Series'>
Index, values 속성과 keys 메서드 사용하기¶
1. Index 속성 사용하기¶
In [27]:
print(first_row.index)
Index(['Occupation', 'Born', 'Died', 'Age'], dtype='object')
2. values 속성 사용하기¶
In [28]:
print(first_row.values)
['Statistician' '1876-06-13' '1937-10-16' 61]
3. keys 메서드 사용하기¶
In [31]:
# keys 는 속성이 아닌 메서드, keys 메서드는 index 속성과 같은 역할을 함
print(first_row.keys())
Index(['Occupation', 'Born', 'Died', 'Age'], dtype='object')
index 속성 응용하기¶
In [30]:
print(first_row.index[0])
Occupation
시리즈의 mean, min, max, std 메서드 사용하기¶
In [32]:
ages = scientists['Age']
print(ages)
Rosaline Franklin 37
William Gosset 61
Name: Age, dtype: int64
In [44]:
ages.mean() # 평균
Out[44]:
49.0
In [50]:
ages.min() # 최솟값
Out[50]:
37
In [49]:
ages.max() # 최댓값
Out[49]:
61
In [48]:
ages.std() # 표준편차
Out[48]:
16.97056274847714
반응형
'데이터분석' 카테고리의 다른 글
[23.06.13] Python 클래스 - 08(1) (0) | 2023.06.13 |
---|---|
[23.06.12] Python Series, DataFrame 문제 - 07(5) (0) | 2023.06.12 |
[23.06.12] Python pandas - 07(3) (0) | 2023.06.12 |
[23.06.12] Python pandas - 07(2) (1) | 2023.06.12 |
[23.06.12] Python pandas - 07(1) (0) | 2023.06.12 |