Python pandas使用map, apply和applymap实现对DataFrame进行单列/行,多列/行,以及所有元素的操作


最近在查看网上关于pandas DataFrame使用map, apply和applymap的说明时,发现许多博文未能写清楚关键点。这里整理一下每个函数的使用范围和适用情况。

map:只可以实现对DataFrame中单列或单行的操作

首先明确一点,DataFrame是没有map函数的,只有Series有。DataFrame的单列/行是一个Series,所以map可以实现单列/行的操作。

例1,字典替换DataFrame中的行或者列

以下是使用举例:用字典替换列a中的值,以及对行2进行判断填充。

df = pd.DataFrame(np.arange(12).reshape(3,4), columns=list('abcd'))
df
>>> a	b	c	d
0	0	1	2	3
1	4	5	6	7
2	8	9	10	11

# 用字典替换列`a`中的值
df['a'] = df['a'].map({0:0, 4:10, 8:4})
df
>>> a	b	c	d
0	0	1	2	3
1	10	5	6	7
2	4	9	10	11

# 对行`2`进行判断
df.loc[2] = df.loc[2].map(lambda x: x if x>9 else 9-x)
df
>>>	a	b	c	d
0	0	1	2	3
1	10	5	6	7
2	5	0	10	11

例2,groupby得到的字典替换DataFrame中groupby的列

df = pd.DataFrame({'c1':['a', 'a', 'a', 'b', 'b', 'b'],
                  'c2':['c', 'd', 'd', 'e', 'f', 'f'],
                  'c3':[1, 2, 3, 4, 5, 6],
                   'c4': np.random.randn(6)
                  })
                  
df.groupby('c1')['c3'].mean()
>>> c1
a    2
b    5
Name: c3, dtype: int64              

df['c1'].map(df.groupby('c1')['c3'].mean())
>>>0    2
1    2
2    2
3    5
4    5
5    5
Name: c1, dtype: int64

apply: 可以实现对DataFrame的多列或多行的操作

DataFrame的apply函数可以实现对多列,多行的操作。需要记住的是,参数axis设为1是对列进行操作,参数axis设为0是对行操作。默认是对行操作。 以下是多列和多行操作的举例:

现有如下一个DataFrame:

np.random.seed(1)
df = pd.DataFrame(np.random.randn(4,2), columns=['A', 'B'])
df
>>>       A	    B
0	1.624345	-0.611756
1	-0.528172	-1.072969
2	0.865408	-2.301539
3	1.744812	-0.761207

对多列进行操作
A, B两列操作,生成C列, 其中C是字符串,由A ± B组成

df['C'] = df.apply(lambda x: '{:.2f}±{:.2f}'.format(x['A'], x['B']), axis=1)
df
>>> 	A	B	C
0	1.624345	-0.611756	1.62±-0.61
1	-0.528172	-1.072969	-0.53±-1.07
2	0.865408	-2.301539	0.87±-2.30
3	1.744812	-0.761207	1.74±-0.76

result_type=expand 的神奇用法

apply中有一个参数是result_type,如果将其设成expand,可以返回多列/多行结果。具体看以下实例:

import numpy as np
import pandas as pd
df = pd.DataFrame(np.arange(12).reshape(3,4), columns=list('abcd'))

# expand, 返回dataframe
df.apply(lambda x:[x['a']+1, x['c']+2], axis=1, result_type="expand") # 每行返回一个list
>>> 0	1
0	1	3
1	5	7
2	9	11

# reduce, 返回Series
df.apply(lambda x:[x['a']+1, x['c']+2], axis=1, result_type="reduce")
>>>
0     [1, 4]
1     [5, 8]
2    [9, 12]
dtype: object

# expand, 返回dataframe
df.apply(lambda x:(x[0]+1, x[1]+2), axis=0, result_type="expand")
>>> a	b	c	d
0	1	2	3	4
1	6	7	8	9

对多行进行操作

df.loc[10] = df.apply(lambda x: '{:.2f}±{:.2f}'.format(x[2], x[3]) )
df
>>>        A	B
0	1.624345	-0.611756
1	-0.528172	-1.072969
2	0.865408	-2.301539
3	1.744812	-0.761207
10	0.87±1.74	-2.30±-0.76

applymap: 可以实现对DataFrame中所有元素的整体操作

如果是对所有元素进行一个操作的话,可以使用applymap

df = pd.DataFrame(np.arange(12).reshape(3,4), columns=list('abcd'))
df = df.applymap(lambda x: x if x>6 else 6-x)
df
>>> a	b	c	d
0	6	5	4	3
1	2	1	0	7
2	8	9	10	11
使用Pandas处理大型DataFrame时,mapapplyapplymap函数是数据映射批量处理的关键工具。map函数通常用于Series对象,对其中的每个元素应用同一个映射规则;apply函数则可以用于DataFrame的每一或每一列,执更为复杂的操作applymap函数适用于DataFrame中的每个元素,包括数字、字符串缺失值。 参考资源链接:[Pandas高效数据处理:mapapplyapplymap函数详解](https://wenku.csdn.net/doc/19qi0f9abb?spm=1055.2569.3001.10343) 首先,推荐查阅《Pandas高效数据处理:mapapplyapplymap函数详解》,这本书详细介绍了这三个函数的使用方法场景,适合希望通过实践提高数据处理效率的用户。 以一个简单的例子来说明如何使用map函数进数据映射。假设我们有一个包含商品名称价格的DataFrame,我们希望根据商品类别给价格添加特定的前缀: ```python import pandas as pd # 创建一个示例DataFrame data = pd.DataFrame({ 'Product': ['Keyboard', 'Mouse', 'Monitor', 'Webcam'], 'Category': ['Electronics', 'Electronics', 'Electronics', 'Computing'], 'Price': [110, 50, 130, 40] }) # 创建一个映射字典 category_to_prefix = { 'Electronics': 'E-', 'Computing': 'C-' } # 使用map函数添加前缀 data['Prefixed Price'] = data['Category'].map(category_to_prefix) + data['Price'].astype(str) print(data) ``` 在这个例子中,我们通过map函数将'Category'列中的每个值映射到相应的前缀,并与'Price'列的值连接起来,从而实现了批量处理。map函数通过映射规则直接对Series中的每个元素操作,极大地提高了代码的执效率可读性。 如果需要进更复杂的操作,比如根据多个列的值来计算新列的值,apply函数将更适合。而applymap函数则适用于需要对DataFrame中的每个元素统一操作的情况,无论元素是数字、字符串还是缺失值。 为了深入理解这些函数的使用,以及如何根据不同的数据结构业务需求选择合适的函数,继续参考《Pandas高效数据处理:mapapplyapplymap函数详解》将会获得更多的实践技巧深入理解。 参考资源链接:[Pandas高效数据处理:mapapplyapplymap函数详解](https://wenku.csdn.net/doc/19qi0f9abb?spm=1055.2569.3001.10343)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值