[파이썬] 파이썬 데이터 사이언스 핸드북 정리(Pandas로 데이터 가공하기 #1)


파이썬 데이터 사이언스 핸드북 -제이크 밴더플래스- -위키북스- 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
data.values
array([0.25, 0.5 , 0.75, 1.  ])
data.index
RangeIndex(start=0, stop=4, step=1)
data[1:3]
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']
0.25

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([2,4,6])
0    2
1    4
2    6
dtype: int64
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
populationarea
California38332521423967
Texas26448193695662
New York19651127141297
Florida19552860170312
Illinois12882135149995

데이터 인덱싱과 선택

인덱서 : 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.loc[1]
'a'
data.iloc[1]
'b'

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
areapop
California42396738332521
Texas69566226448193
New York14129719651127
Florida17031219552860
Illinois14999512882135
# 열을 이름으로 인덱싱
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
areapopdensity
California4239673833252190.413926
Texas6956622644819338.018740
New York14129719651127139.076746
Florida17031219552860114.806121
Illinois1499951288213585.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]])
# 행열 바꾸기
data.T
CaliforniaTexasNew YorkFloridaIllinois
area4.239670e+056.956620e+051.412970e+051.703120e+051.499950e+05
pop3.833252e+072.644819e+071.965113e+071.955286e+071.288214e+07
density9.041393e+013.801874e+011.390767e+021.148061e+028.588376e+01
data.values[0]
array([4.23967000e+05, 3.83325210e+07, 9.04139261e+01])
data['area']
California    423967
Texas         695662
New York      141297
Florida       170312
Illinois      149995
Name: area, dtype: int64
# DataFrame 인덱스와 열 레이블 결과 유지 iloc 이용
data.iloc[:3, :2]
areapop
California42396738332521
Texas69566226448193
New York14129719651127
# loc으로 이용
data.loc[:'Illinois', :'pop']
areapop
California42396738332521
Texas69566226448193
New York14129719651127
Florida17031219552860
Illinois14999512882135
# 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)
areapop
California42396738332521
Texas69566226448193
New York14129719651127
# 마스킹과 패신 인덱싱을 결합 할 수 있음
data.loc[data.density > 100, ['pop', 'density']]
popdensity
New York19651127139.076746
Florida19552860114.806121
# 값 변경에도 iloc 쓰임
data.iloc[0, 2] = 90
data
areapopdensity
California4239673833252190.000000
Texas6956622644819338.018740
New York14129719651127139.076746
Florida17031219552860114.806121
Illinois1499951288213585.883763

추가적인 인덱싱 규칙

# 인덱싱은 열을 참조하지만 슬라이싱은 행을 참조한다.(헷갈리네)
data['Florida':'Illinois']
areapopdensity
Florida17031219552860114.806121
Illinois1499951288213585.883763
data[1:3]
areapopdensity
Texas6956622644819338.018740
New York14129719651127139.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
ABCD
06926
17437
27254
np.exp(ser)
0     403.428793
1      20.085537
2    1096.633158
3      54.598150
dtype: float64
np.sin(df * np.pi / 4)
ABCD
0-1.0000007.071068e-011.000000-1.000000e+00
1-0.7071071.224647e-160.707107-7.071068e-01
2-0.7071071.000000e+00-0.7071071.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
AB
0111
151
B = pd.DataFrame(rng.randint(0, 10, (3, 3)),
                 columns=list('BAC'))
B
BAC
0382
1426
2486
A+B
ABC
09.014.0NaN
17.05.0NaN
2NaNNaNNaN
fill = A.stack().mean()
A.add(B, fill_value=fill)
ABC
09.014.06.5
17.05.010.5
212.58.510.5

유니버설 함수 : DataFrame과 Series 간의 연산

A = rng.randint(10, size=(3, 4))
A
array([[2, 0, 3, 1],
       [7, 3, 1, 5],
       [5, 9, 3, 5]])
# 브로드 캐스팅
A-A[0]
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]
QRST
00000
153-24
23904
df.subtract(df['R'], axis=0)
QRST
02031
140-22
2-40-6-4






© 2018. by statssy

Powered by statssy