6. 함수적용과 매핑
페이지 정보
작성자 관리자 댓글 0건 조회 1,913회 작성일 20-01-23 21:37본문
numpy.random모듈에 있는 randn함수는 임의의 정규분표 데이터를 생성한다.
실습1.
from pandas import Series, DataFrame
import pandas as pd
import numpy as np
df = DataFrame(np.random.randn(4,3), columns=list('bde'),
index=['seoul', 'busan', 'daegu', 'incheon'])
print(df)
[결과]
b d e
seoul -0.690852 -1.068849 -0.508846
busan -0.742989 -0.408708 1.532402
daegu 0.123536 -1.114255 -1.044610
incheon 0.697526 -1.806242 1.154230
실습2.
from pandas import Series, DataFrame
import pandas as pd
import numpy as np
df = DataFrame(np.random.randn(4,3), columns=list('bde'),
index=['seoul', 'busan', 'daegu', 'incheon'])
print(df)
# 절대값
print(np.abs(df))
[결과]
b d e
seoul -0.121970 -0.724825 -0.832105
busan -1.422632 -0.609767 -0.044190
daegu 1.062511 -0.004625 -1.441716
incheon -0.574567 -0.042098 0.282086
b d e
seoul 0.121970 0.724825 0.832105
busan 1.422632 0.609767 0.044190
daegu 1.062511 0.004625 1.441716
incheon 0.574567 0.042098 0.282086
실습3.
from pandas import Series, DataFrame
import pandas as pd
import numpy as np
df = DataFrame(np.random.randn(4,3), columns=list('bde'),
index=['seoul', 'busan', 'daegu', 'incheon'])
print(df)
f=lambda x:x.max()-x.min()
print(df.apply(f))
print(df.apply(f,axis=1))
[결과]
b d e
seoul -0.250209 1.642199 1.420806
busan 1.628789 0.907858 0.475666
daegu 0.208384 -1.445009 0.989063
incheon -1.318783 0.742646 -1.212848
b 2.947572
d 3.087208
e 2.633654
dtype: float64
seoul 1.892407
busan 1.153123
daegu 2.434073
incheon 2.061429
dtype: float64
댓글목록
등록된 댓글이 없습니다.