파이썬 데이터 사이언스 핸드북 -제이크 밴더플래스- -위키북스- P111 ~ P136까지 (Pandas로 데이터 가공하기)
import numpy as np
import pandas as pd
Pandas Series 객체
data = pd.Series([0.25, 0.5, 0.75, 1.0])
data
0 0.25
1 0.50
2 0.75
3 1.00
dtype: float64
array([0.25, 0.5 , 0.75, 1. ])
RangeIndex(start=0, stop=4, step=1)
1 0.50
2 0.75
dtype: float64
Series : 일반화된 Numpy 배열
data = pd.Series([0.25, 0.5, 0.75, 1.0],
index = ['a', 'b', 'c', 'd'])
data
a 0.25
b 0.50
c 0.75
d 1.00
dtype: float64
# 인덱스로 접근할 수 있다.
data['a']
Series : 특수한 딕셔너리
- 딕셔너리 방식으로 Series를 만들수 있다.
population_dict = {'California' : 38332521,
'Texas' : 26448193,
'New York' : 19651127,
'Florida' : 19552860,
'Illinois' : 12882135}
population = pd.Series(population_dict)
population
California 38332521
Texas 26448193
New York 19651127
Florida 19552860
Illinois 12882135
dtype: int64
# 당연히 Series로 바꿨으니 슬라이싱 가능하다.
population['New York':'Illinois']
New York 19651127
Florida 19552860
Illinois 12882135
dtype: int64
Series 객체 구성하기
pd.Series(5, index=[100, 200, 300])
100 5
200 5
300 5
dtype: int64
pd.Series({2:'a', 1:'b', 3:'c' })
2 a
1 b
3 c
dtype: object
Pandas DataFrame 객체
DataFrame : 일반화된 Numpy 배열
area_dict = {'California' : 423967, 'Texas' : 695662, 'New York' : 141297,
'Florida' : 170312, 'Illinois' : 149995}
area = pd.Series(area_dict)
area
California 423967
Texas 695662
New York 141297
Florida 170312
Illinois 149995
dtype: int64
states = pd.DataFrame({'population' : population,
'area' : area})
states
| population | area |
---|
California | 38332521 | 423967 |
---|
Texas | 26448193 | 695662 |
---|
New York | 19651127 | 141297 |
---|
Florida | 19552860 | 170312 |
---|
Illinois | 12882135 | 149995 |
---|
데이터 인덱싱과 선택
인덱서 : loc, iloc, ix
- loc : 지정한 인덱스 글자
- iloc : 고유 인덱스 순서
data = pd.Series(['a', 'b', 'c'], index = [1, 3, 5])
data
1 a
3 b
5 c
dtype: object
Data Frame에서 데이터 선택
area = pd.Series({'California': 423967, 'Texas' : 695662,
'New York' : 141297, 'Florida' : 170312,
'Illinois' : 149995})
pop = pd.Series({'California' : 38332521, 'Texas' : 26448193,
'New York' : 19651127, 'Florida' : 19552860,
'Illinois' : 12882135})
data = pd.DataFrame({'area' : area, 'pop' : pop})
data
| area | pop |
---|
California | 423967 | 38332521 |
---|
Texas | 695662 | 26448193 |
---|
New York | 141297 | 19651127 |
---|
Florida | 170312 | 19552860 |
---|
Illinois | 149995 | 12882135 |
---|
# 열을 이름으로 인덱싱
data['area']
California 423967
Texas 695662
New York 141297
Florida 170312
Illinois 149995
Name: area, dtype: int64
# 속성 스타일로 인덱싱 가능
data.area
California 423967
Texas 695662
New York 141297
Florida 170312
Illinois 149995
Name: area, dtype: int64
# 그런데 pop 같이 메서드가 있는건 쓰면 안되기 떄문에 그냥 인덱싱 하는걸 손에 익히는게 좋겠다
data['density'] = data['pop']/data['area']
data
| area | pop | density |
---|
California | 423967 | 38332521 | 90.413926 |
---|
Texas | 695662 | 26448193 | 38.018740 |
---|
New York | 141297 | 19651127 | 139.076746 |
---|
Florida | 170312 | 19552860 | 114.806121 |
---|
Illinois | 149995 | 12882135 | 85.883763 |
---|
DataFrame : 2차원 배열
# value를 이용하면 원시 데이터를 볼 수 있다.
data.values
array([[4.23967000e+05, 3.83325210e+07, 9.04139261e+01],
[6.95662000e+05, 2.64481930e+07, 3.80187404e+01],
[1.41297000e+05, 1.96511270e+07, 1.39076746e+02],
[1.70312000e+05, 1.95528600e+07, 1.14806121e+02],
[1.49995000e+05, 1.28821350e+07, 8.58837628e+01]])
| California | Texas | New York | Florida | Illinois |
---|
area | 4.239670e+05 | 6.956620e+05 | 1.412970e+05 | 1.703120e+05 | 1.499950e+05 |
---|
pop | 3.833252e+07 | 2.644819e+07 | 1.965113e+07 | 1.955286e+07 | 1.288214e+07 |
---|
density | 9.041393e+01 | 3.801874e+01 | 1.390767e+02 | 1.148061e+02 | 8.588376e+01 |
---|
array([4.23967000e+05, 3.83325210e+07, 9.04139261e+01])
California 423967
Texas 695662
New York 141297
Florida 170312
Illinois 149995
Name: area, dtype: int64
# DataFrame 인덱스와 열 레이블 결과 유지 iloc 이용
data.iloc[:3, :2]
| area | pop |
---|
California | 423967 | 38332521 |
---|
Texas | 695662 | 26448193 |
---|
New York | 141297 | 19651127 |
---|
# loc으로 이용
data.loc[:'Illinois', :'pop']
| area | pop |
---|
California | 423967 | 38332521 |
---|
Texas | 695662 | 26448193 |
---|
New York | 141297 | 19651127 |
---|
Florida | 170312 | 19552860 |
---|
Illinois | 149995 | 12882135 |
---|
# ix는 iloc loc 방식의 하이브리드 형태
data.ix[:3, :'pop']
d:\python_study\python_handbook\venv\lib\site-packages\ipykernel_launcher.py:2: FutureWarning:
.ix is deprecated. Please use
.loc for label based indexing or
.iloc for positional indexing
See the documentation here:
http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#ix-indexer-is-deprecated
d:\python_study\python_handbook\venv\lib\site-packages\pandas\core\indexing.py:822: FutureWarning:
.ix is deprecated. Please use
.loc for label based indexing or
.iloc for positional indexing
See the documentation here:
http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#ix-indexer-is-deprecated
retval = getattr(retval, self.name)._getitem_axis(key, axis=i)
| area | pop |
---|
California | 423967 | 38332521 |
---|
Texas | 695662 | 26448193 |
---|
New York | 141297 | 19651127 |
---|
# 마스킹과 패신 인덱싱을 결합 할 수 있음
data.loc[data.density > 100, ['pop', 'density']]
| pop | density |
---|
New York | 19651127 | 139.076746 |
---|
Florida | 19552860 | 114.806121 |
---|
# 값 변경에도 iloc 쓰임
data.iloc[0, 2] = 90
data
| area | pop | density |
---|
California | 423967 | 38332521 | 90.000000 |
---|
Texas | 695662 | 26448193 | 38.018740 |
---|
New York | 141297 | 19651127 | 139.076746 |
---|
Florida | 170312 | 19552860 | 114.806121 |
---|
Illinois | 149995 | 12882135 | 85.883763 |
---|
추가적인 인덱싱 규칙
# 인덱싱은 열을 참조하지만 슬라이싱은 행을 참조한다.(헷갈리네)
data['Florida':'Illinois']
| area | pop | density |
---|
Florida | 170312 | 19552860 | 114.806121 |
---|
Illinois | 149995 | 12882135 | 85.883763 |
---|
| area | pop | density |
---|
Texas | 695662 | 26448193 | 38.018740 |
---|
New York | 141297 | 19651127 | 139.076746 |
---|
Pandas에서 데이터 연산하기
유니버설 함수 : 인덱스 보존
import pandas as pd
import numpy as np
rng = np.random.RandomState(42)
ser = pd.Series(rng.randint(0, 10, 4))
ser
0 6
1 3
2 7
3 4
dtype: int32
df = pd.DataFrame(rng.randint(0, 10, (3, 4)),
columns=['A','B','C','D'])
df
0 403.428793
1 20.085537
2 1096.633158
3 54.598150
dtype: float64
| A | B | C | D |
---|
0 | -1.000000 | 7.071068e-01 | 1.000000 | -1.000000e+00 |
---|
1 | -0.707107 | 1.224647e-16 | 0.707107 | -7.071068e-01 |
---|
2 | -0.707107 | 1.000000e+00 | -0.707107 | 1.224647e-16 |
---|
유니버설 함수 : 인덱스 정렬
Series에서 인덱스 정렬
A = pd.Series([2, 4, 6], index = [0, 1, 2])
B = pd.Series([1, 3, 5], index = [1, 2, 3])
A+B
0 NaN
1 5.0
2 9.0
3 NaN
dtype: float64
# NaN이 안나오게 하기
A.add(B, fill_value=0)
0 2.0
1 5.0
2 9.0
3 5.0
dtype: float64
DataFrame에서 인덱스 정렬
A = pd.DataFrame(rng.randint(0, 20, (2, 2)),
columns=list('AB'))
A
B = pd.DataFrame(rng.randint(0, 10, (3, 3)),
columns=list('BAC'))
B
| A | B | C |
---|
0 | 9.0 | 14.0 | NaN |
---|
1 | 7.0 | 5.0 | NaN |
---|
2 | NaN | NaN | NaN |
---|
fill = A.stack().mean()
A.add(B, fill_value=fill)
| A | B | C |
---|
0 | 9.0 | 14.0 | 6.5 |
---|
1 | 7.0 | 5.0 | 10.5 |
---|
2 | 12.5 | 8.5 | 10.5 |
---|
유니버설 함수 : DataFrame과 Series 간의 연산
A = rng.randint(10, size=(3, 4))
A
array([[2, 0, 3, 1],
[7, 3, 1, 5],
[5, 9, 3, 5]])
array([[ 0, 0, 0, 0],
[ 5, 3, -2, 4],
[ 3, 9, 0, 4]])
# Pandas에서도 이렇게 쓸 수 있다.
df = pd.DataFrame(A, columns=list('QRST'))
df - df.iloc[0]
df.subtract(df['R'], axis=0)