pandas II

pandas II


selection & drop

In [1]:
import pandas as pd
from pandas import Series
from pandas import DataFrame
import numpy as np
In [2]:
raw_data = {
    "first_name": ["Jason", "Molly", "Tina", "Jake", "Amy"],
    "last_name": ["Miller", "Jacobson", "Ali", "Milner", "Cooze"],
    "age": [42, 52, 36, 24, 73],
    "city": ["San Francisco", "Baltimore", "Miami", "Douglas", "Boston"],
}
df = pd.DataFrame(raw_data)
df
Out[2]:
first_name last_name age city
0 Jason Miller 42 San Francisco
1 Molly Jacobson 52 Baltimore
2 Tina Ali 36 Miami
3 Jake Milner 24 Douglas
4 Amy Cooze 73 Boston

한개의 column선택

In [3]:
df['first_name'].head(3)
Out[3]:
0    Jason
1    Molly
2     Tina
Name: first_name, dtype: object

1개 이상의 column선택

In [4]:
df[['first_name','last_name','city']]
Out[4]:
first_name last_name city
0 Jason Miller San Francisco
1 Molly Jacobson Baltimore
2 Tina Ali Miami
3 Jake Milner Douglas
4 Amy Cooze Boston

컬럼이 많을때 보기 편하게

In [5]:
df.head(2).T
Out[5]:
0 1
first_name Jason Molly
last_name Miller Jacobson
age 42 52
city San Francisco Baltimore
In [6]:
display(df['age'])
display(df[['age']])
0    42
1    52
2    36
3    24
4    73
Name: age, dtype: int64
age
0 42
1 52
2 36
3 24
4 73
  • 컬럼을 하나 뽑을때, []하나만 사용하면 Series로 뽑힌다. []두개 사용하면 DataFrame으로 뽑힌다.
In [7]:
display(df[:3])
display(df['city'][:2])   # Series로 뽑힌다.
first_name last_name age city
0 Jason Miller 42 San Francisco
1 Molly Jacobson 52 Baltimore
2 Tina Ali 36 Miami
0    San Francisco
1        Baltimore
Name: city, dtype: object
  • column 이름 없이 사용하는 index number는 row 기준
In [8]:
age_series=df['age']
age_series[[0,2,4]]
Out[8]:
0    42
2    36
4    73
Name: age, dtype: int64
  • Series에서 1개 이상의 index사용
In [9]:
age_series[age_series>40]
Out[9]:
0    42
1    52
4    73
Name: age, dtype: int64
  • Boolean index

index 설정

In [10]:
display(df.set_index('city'))
display(df)
first_name last_name age
city
San Francisco Jason Miller 42
Baltimore Molly Jacobson 52
Miami Tina Ali 36
Douglas Jake Milner 24
Boston Amy Cooze 73
first_name last_name age city
0 Jason Miller 42 San Francisco
1 Molly Jacobson 52 Baltimore
2 Tina Ali 36 Miami
3 Jake Milner 24 Douglas
4 Amy Cooze 73 Boston
  • 원본 데이터를 바꾸진 않는다. 저 DataFrame을 쓰고 싶으면 새로 변수를 할당한다.


data drop

In [11]:
df.drop(1)
Out[11]:
first_name last_name age city
0 Jason Miller 42 San Francisco
2 Tina Ali 36 Miami
3 Jake Milner 24 Douglas
4 Amy Cooze 73 Boston
  • index number로 drop한다.
In [12]:
df.drop([0,2,3])
Out[12]:
first_name last_name age city
1 Molly Jacobson 52 Baltimore
4 Amy Cooze 73 Boston
  • 한개 이상의 Index number로 drop
In [13]:
df.drop('city',axis=1)
Out[13]:
first_name last_name age
0 Jason Miller 42
1 Molly Jacobson 52
2 Tina Ali 36
3 Jake Milner 24
4 Amy Cooze 73
  • axis지정으로 축을 기준으로 drop. 기본은 axis=0행을 의미 한다.

Series Operation

In [14]:
s1 = Series(range(1, 6), index=list("abced"))
s1
Out[14]:
a    1
b    2
c    3
e    4
d    5
dtype: int64
In [15]:
s2 = Series(range(5, 11), index=list("bcedef"))
s2
Out[15]:
b     5
c     6
e     7
d     8
e     9
f    10
dtype: int64
In [16]:
s1+s2
Out[16]:
a     NaN
b     7.0
c     9.0
d    13.0
e    11.0
e    13.0
f     NaN
dtype: float64
  • index으로 기준으로 연산을 수행한다. 이때 겹치는 index가 없을 경우 NaN값으로 반환


DataFrame Operation

In [17]:
df1 = DataFrame(np.arange(9).reshape(3, 3), columns=list("abc"))
df1
Out[17]:
a b c
0 0 1 2
1 3 4 5
2 6 7 8
In [18]:
df2 = DataFrame(np.arange(16).reshape(4, 4), columns=list("abcd"))
df2
Out[18]:
a b c d
0 0 1 2 3
1 4 5 6 7
2 8 9 10 11
3 12 13 14 15
In [19]:
df1+df2
Out[19]:
a b c d
0 0.0 2.0 4.0 NaN
1 7.0 9.0 11.0 NaN
2 14.0 16.0 18.0 NaN
3 NaN NaN NaN NaN
In [20]:
(df1+df2).fillna(0)
Out[20]:
a b c d
0 0.0 2.0 4.0 0.0
1 7.0 9.0 11.0 0.0
2 14.0 16.0 18.0 0.0
3 0.0 0.0 0.0 0.0
  • DataFrame은 column과 index를 모두 고려한다.
  • NaN값을 처리하기 위해서 fillna()메서드를 사용한다.


Series+DataFrame

In [21]:
df = DataFrame(np.arange(16).reshape(4, 4), columns=list("abcd"))
df
Out[21]:
a b c d
0 0 1 2 3
1 4 5 6 7
2 8 9 10 11
3 12 13 14 15
In [22]:
s = Series(np.arange(10, 14), index=list("abcd"))
s
Out[22]:
a    10
b    11
c    12
d    13
dtype: int32
In [23]:
df+s
Out[23]:
a b c d
0 10 12 14 16
1 14 16 18 20
2 18 20 22 24
3 22 24 26 28
  • brodcasting이 일어나서 연산이 진행된다.



lambda & map & apply

map

  • pandas의 series type의 데이터에도 map함수 사용가능
  • function대신 dict, sequence형 자료등으로 대체가능
In [24]:
s1=Series(np.arange(10))
s1.head(5)
Out[24]:
0    0
1    1
2    2
3    3
4    4
dtype: int32
In [25]:
s1.map(lambda x: x**2).head()
Out[25]:
0     0
1     1
2     4
3     9
4    16
dtype: int64
In [26]:
z = {1: "A", 2: "B", 3: "C"}
s1.map(z)
Out[26]:
0    NaN
1      A
2      B
3      C
4    NaN
5    NaN
6    NaN
7    NaN
8    NaN
9    NaN
dtype: object
  • dict type으로 데이터 교체가 일어난다. 이때 없는 값은 NaN값으로 바뀐다.
In [27]:
s2 = Series(np.arange(10, 30))
s1.map(s2)
Out[27]:
0    10
1    11
2    12
3    13
4    14
5    15
6    16
7    17
8    18
9    19
dtype: int32
  • index가 같은 위치의 값은 s2값으로 바뀐다.

예시

In [28]:
df=pd.read_csv('./data/wages.csv')
df.head()
Out[28]:
earn height sex race ed age
0 79571.299011 73.89 male white 16 49
1 96396.988643 66.23 female white 16 62
2 48710.666947 63.77 female white 16 33
3 80478.096153 63.22 female other 16 95
4 82089.345498 63.08 female white 17 43
In [29]:
df['sex'].unique()
Out[29]:
array(['male', 'female'], dtype=object)
  • .unique()메서드는 중복 없이 값만 보여준다.
In [30]:
df['sex_code']=df['sex'].map({'male':0,'female':1})
df.head()
Out[30]:
earn height sex race ed age sex_code
0 79571.299011 73.89 male white 16 49 0
1 96396.988643 66.23 female white 16 62 1
2 48710.666947 63.77 female white 16 33 1
3 80478.096153 63.22 female other 16 95 1
4 82089.345498 63.08 female white 17 43 1
In [31]:
def change_sex(x):
    return 0 if x == "male" else 1

df.sex.map(change_sex).head()
Out[31]:
0    0
1    1
2    1
3    1
4    1
Name: sex, dtype: int64


replace

  • Map 함수의 기능중 데이터 변환 기능만 담당
  • 데이터 변환시 많이 사용하는 함수
In [32]:
df.sex.replace({"male": 0, "female": 1}).head()
Out[32]:
0    0
1    1
2    1
3    1
4    1
Name: sex, dtype: int64
  • dict type적용
In [33]:
df.sex.replace(["male", "female"], [0, 1], inplace=True)
df.head()
Out[33]:
earn height sex race ed age sex_code
0 79571.299011 73.89 0 white 16 49 0
1 96396.988643 66.23 1 white 16 62 1
2 48710.666947 63.77 1 white 16 33 1
3 80478.096153 63.22 1 other 16 95 1
4 82089.345498 63.08 1 white 17 43 1
  • Targe listConversion list를 인자로 사용할 수 있다.
  • inplace인자는 데이터 변환결과를 직접 적용하겠다는 의미이다.


apply

  • 내장 연산 함수를 사용할 때도 똑같은 효과를 거둘 수 있음
  • mean, std등 사용가능
In [34]:
df_info = df[["earn", "height", "age"]]
df_info.sum()
Out[34]:
earn      4.474344e+07
height    9.183125e+04
age       6.250800e+04
dtype: float64
In [35]:
df_info.apply(sum)
Out[35]:
earn      4.474344e+07
height    9.183125e+04
age       6.250800e+04
dtype: float64
In [36]:
def f(x):
    return Series(
        [x.min(), x.max(), x.mean(), sum(x.isnull())],
        index=["min", "max", "mean", "null"])

df_info.apply(f)
Out[36]:
earn height age
min -98.580489 57.34000 22.000000
max 317949.127955 77.21000 95.000000
mean 32446.292622 66.59264 45.328499
null 0.000000 0.00000 0.000000
  • Scalar값 이외에 Series값의 반환도 가능
  • applycolumn별로 순회된다고 생각.
  • x에 들어오는 인자는 DataFrame의 column들이다.


applymap

  • series 단위가 아닌 element 단위로 함수를 적용함
  • series 단위에 apply를 적용시킬 때와 같은효과
In [37]:
f = lambda x: x // 2
df_info.applymap(f).head(5)
Out[37]:
earn height age
0 39785.0 36.0 24
1 48198.0 33.0 31
2 24355.0 31.0 16
3 40239.0 31.0 47
4 41044.0 31.0 21
In [38]:
f = lambda x: x ** 2
df_info["earn"].apply(f)
Out[38]:
0       6.331592e+09
1       9.292379e+09
2       2.372729e+09
3       6.476724e+09
4       6.738661e+09
            ...     
1374    9.104329e+08
1375    6.176974e+08
1376    1.879825e+08
1377    9.106124e+09
1378    9.168947e+07
Name: earn, Length: 1379, dtype: float64



pandas built-in functions

describe

  • Numeric type 데이터의 요약 정보를 보여줌
In [39]:
df = pd.read_csv("data/wages.csv")
df.head()
Out[39]:
earn height sex race ed age
0 79571.299011 73.89 male white 16 49
1 96396.988643 66.23 female white 16 62
2 48710.666947 63.77 female white 16 33
3 80478.096153 63.22 female other 16 95
4 82089.345498 63.08 female white 17 43
In [40]:
df.describe()
Out[40]:
earn height ed age
count 1379.000000 1379.000000 1379.000000 1379.000000
mean 32446.292622 66.592640 13.354605 45.328499
std 31257.070006 3.818108 2.438741 15.789715
min -98.580489 57.340000 3.000000 22.000000
25% 10538.790721 63.720000 12.000000 33.000000
50% 26877.870178 66.050000 13.000000 42.000000
75% 44506.215336 69.315000 15.000000 55.000000
max 317949.127955 77.210000 18.000000 95.000000


unique

  • Series data의 유일한 값을 array로 반환
In [41]:
df.race.unique()
Out[41]:
array(['white', 'other', 'hispanic', 'black'], dtype=object)
In [42]:
dict(enumerate(df["race"].unique()))
Out[42]:
{0: 'white', 1: 'other', 2: 'hispanic', 3: 'black'}
  • dict type으로 index할 수 있다.
In [43]:
value = list(map(int, np.array(list(enumerate(df["race"].unique())))[:, 0].tolist()))
key = np.array(list(enumerate(df["race"].unique())), dtype=str)[:, 1].tolist()

value, key
Out[43]:
([0, 1, 2, 3], ['white', 'other', 'hispanic', 'black'])
  • array에서 .tolist()메서드는 arraylist로 바꿔준다.
In [44]:
df["race"].replace(to_replace=key, value=value)
Out[44]:
0       0
1       0
2       0
3       1
4       0
       ..
1374    0
1375    0
1376    0
1377    0
1378    0
Name: race, Length: 1379, dtype: int64
  • 이 값들은 위와 같이 race컬럼에 있는 값들은 index를 메기고 해당 label로 변환하는데 사용할 수 있다.


sum

  • 기본적인 column 또는 row 값의 연산을 지원
  • sub, mean, min, max, count, median, mad, var 등
In [45]:
# column별
df.sum(axis=0)
Out[45]:
earn                                            4.47434e+07
height                                              91831.2
sex       malefemalefemalefemalefemalefemalefemalemalema...
race      whitewhitewhiteotherwhitewhitewhitewhitehispan...
ed                                                    18416
age                                                   62508
dtype: object
In [46]:
# row별
df.sum(axis=1)
Out[46]:
0       79710.189011
1       96541.218643
2       48823.436947
3       80652.316153
4       82212.425498
            ...     
1374    30290.060363
1375    25018.829514
1376    13823.311312
1377    95563.664410
1378     9686.681857
Length: 1379, dtype: float64


isnull

  • column 또는row 값의 NaN(null) 값의 index를 반환함
In [47]:
df.isnull().head()
Out[47]:
earn height sex race ed age
0 False False False False False False
1 False False False False False False
2 False False False False False False
3 False False False False False False
4 False False False False False False
In [48]:
df.isnull().sum()
Out[48]:
earn      0
height    0
sex       0
race      0
ed        0
age       0
dtype: int64
  • Null인 값의 합


sort_values

  • column 값을 기준으로 데이터를 sorting
In [49]:
df.sort_values(["age", "earn"], ascending=True).head()
Out[49]:
earn height sex race ed age
1038 -56.321979 67.81 male hispanic 10 22
800 -27.876819 72.29 male white 12 22
963 -25.655260 68.90 male white 12 22
1105 988.565070 64.71 female white 12 22
801 1000.221504 64.09 female white 12 22
  • ascending: 오름차순


Correlation & covariance

  • 상관계수와 공분산을 구하는 함수
  • corr, cov, corrwith
In [50]:
df.age.corr(df.earn)
Out[50]:
0.07400349177836055
In [51]:
df.age.cov(df.earn)
Out[51]:
36523.6992104089
In [52]:
df.corrwith(df.earn)
Out[52]:
earn      1.000000
height    0.291600
ed        0.350374
age       0.074003
dtype: float64
In [53]:
df.corr()
Out[53]:
earn height ed age
earn 1.000000 0.291600 0.350374 0.074003
height 0.291600 1.000000 0.114047 -0.133727
ed 0.350374 0.114047 1.000000 -0.129802
age 0.074003 -0.133727 -0.129802 1.000000

'AI > 이론' 카테고리의 다른 글

확률론 맛보기  (0) 2021.01.28
Pandas IV  (0) 2021.01.28
Pandas III  (0) 2021.01.28
딥러닝 학습방법 이해하기  (0) 2021.01.27
Pandas I  (0) 2021.01.27
경사하강법 II  (0) 2021.01.26
경사하강법 I  (0) 2021.01.26
행렬  (0) 2021.01.25

+ Recent posts