Basics of map function and apply function in python

1. About the map function

>>> map(lambda x: x ** 2, [1, 2, 3, 4, 5])  # 使用 lambda 匿名函数
[1, 4, 9, 16, 25]

2.apply function

import pandas as pd
import numpy as np

matrix = [
    [1,2,3],
    [4,5,6],
    [7,8,9]
]

df = pd.DataFrame(matrix, columns=list('xyz'), index=list('abc'))
df.apply(np.square)

Insert picture description here

df.apply(lambda x:np.square(x) if x.name=='x' else x)

Insert picture description here
Square the df x columns.
By default, axis = 0 means by column, axis = 1 means by row, such as
df.apply (lambda x: np.square (x) if x.name == 'b' else x, axis = 1)

3.drop () function

print(df.drop("a"))

Insert picture description here

print(df.drop("x",axis=1))

Insert picture description here

Published 10 original articles · Likes0 · Visits 83

Guess you like

Origin blog.csdn.net/WangaWen1229/article/details/105472161