Python/Pandas

[Pandas] apply 함수, applymap 함수, map 함수

데이터 세상 2021. 5. 10. 15:29

apply 함수

  • 커스텀 함수를 사용하기 위해 DataFrame에서 복수 개의 컬럼이 필요하다면, apply함수를 사용
  • 열에 있는 모든 원소에 함수를 적용
  • Series에 적용할 경우 각 요소의 값이 적용된다.
data = pd.DataFrame(np.random.randn(4,3), 
                   index=['one','two','three','four'],
                   columns=['seoul','busan','gangju'])
                   
def gf(x):
    return pd.Series([x.mean(), x.std()], index=['mean','std'])

data.apply(gf, axis=0)

  • 매개 변수를 전달할 수도 있다.
frame = pd.read_csv('titanic.csv')

def f1(x, age=40):
    return x < age
frame['Age'].apply(f1, age=30)

  • 그룹핑을 하고 각 group 객체에도 사용할 수 있다.
frame.groupby('Age').apply(lambda x: x.count())
data = pd.Series(np.random.randn(8),
                index=['ohio','new york','vermont','florida',
                      'oregon','nevada','california','idaho'])
data[['vermont','nevada','idaho']] = np.nan

>>
ohio         -0.863630
new york      1.607219
vermont            NaN
florida       0.914322
oregon       -1.011454
nevada             NaN
california    0.624878
idaho              NaN
dtype: float64


labeld = ['east'] * 4 + ['west'] * 4
fill_mean = lambda g: g.fillna(g.mean())
data.groupby(labeld).apply(fill_mean)

>>
ohio         -0.863630
new york      1.607219
vermont       0.552637
florida       0.914322
oregon       -1.011454
nevada       -0.193288
california    0.624878
idaho        -0.193288
dtype: float64

 

applymap 함수

  • DataFrame클래스의 함수이긴 하나, apply함수처럼 각 row(axis=1)나 각 column(axis=0)별로 작동하는 함수가 아니라, 각 요소(element)별로 작동
  • 열에 있는 모든 원소에 함수를 적용
data = pd.DataFrame(np.random.randn(4,3), 
                   index=['one','two','three','four'],
                   columns=['seoul','busan','gangju'])
                   
data.applymap(lambda x: round(x,2))

 

map 함수

  • Series 타입에서만 사용
  • Series 값 하나하나에 접근하면서 해당 함수를 수행
data = pd.DataFrame(np.random.randn(4,3), 
                   index=['one','two','three','four'],
                   columns=['seoul','busan','gangju'])

data['busan'].map(lambda x: round(x,2))

frame = pd.read_csv('titanic.csv')

ddata = {1:'live', 0:'dead'}
frame['Survived'].map(ddata).head(10)
# frame['Survived'].map(lambda x: ddata[x])
반응형

'Python > Pandas' 카테고리의 다른 글

[Pandas] 데이터 정렬하기  (0) 2022.04.26
[Pandas] 데이터프레임 병합  (0) 2022.04.26
[Pandas] 데이터프레임 연결  (0) 2022.04.26
[Pandas] Pandas 개념  (0) 2022.04.25
[Pandas] Pandas-Profiling  (0) 2021.04.12