python中map函数和apply函数基础

1.关于map函数

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

2.apply函数

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)

在这里插入图片描述

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

在这里插入图片描述
给df x列做平方。
默认情况下 axis=0 表示按列,axis=1 表示按行,如
df.apply(lambda x:np.square(x) if x.name==‘b’ else x,axis=1)

3.drop()函数

print(df.drop("a"))

在这里插入图片描述

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

在这里插入图片描述

发布了10 篇原创文章 · 获赞 0 · 访问量 83

猜你喜欢

转载自blog.csdn.net/WangaWen1229/article/details/105472161