apply map applymap

Published by onesixx on

https://zephyrus1111.tistory.com/64

apply

예제 dataframe

df = pd.DataFrame([
    [ 1,  2,  3,  4,  5],
    [ 6,  7,  8,  9, 10],
    [11, 12, 13, 14, 15],
    [16, 17, 18, 19, 20]
], index = ['ridx1', 'ridx2', 'ridx3', 'ridx4'],
   columns=['colNm1', 'colNm2', 'colNm3', 'colNm4', 'colNm5'])

axis 0=>행방향 1=>열방향

# 행기준 (아래로)
df.apply(np.cumsum, axis=0)
# 열기준 (오른쪽으로)
df.apply(np.cumsum, axis=1) 
df['행 합'] = df.sum(axis=1)

df.loc['열 합']= df.sum(axis=0)

df.append(df,ignore_index=True)

rounding values ​​in each column 

df = px.data.stocks()

df.iloc[:, 1:] = df.iloc[:, 1:].apply(lambda x: round(x, 2))

Map

import math
# math.ceil 
# round
# math.floor

list( map(math.ceil, [1.1,2.2,3.3,4.4,5.5,6.6]))
# [2, 3, 4, 5, 6, 7]

lambda

def xxx(x:int):
    return x*x

list( map (xxx, [1,2,3,4,5]) )
list( map(lambda x:x*x, list(range(1,6))) )
# [1, 4, 9, 16, 25]

filter

def isEven(x):
    return x%2==0

list( filter(isEven, [1,2,3,4,5]) )
list( filter(lambda x:x%2==0, list(range(1,6))) )
#[2, 4]

Categories: pandas

onesixx

Blog Owner

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x