map和applymap及apply的区别
1. 数据
import pandas as pd
import numpy as np
frame = pd.DataFrame(np.random.rand(4, 3), columns = list('abc'), index = ['Utah', 'Ohio', 'Texas', 'Oregon'])
print(frame)
# 输出如下:
# a b c
# Utah 0.443188 0.919623 0.550259
# Ohio 0.013923 0.557696 0.723975
# Texas 0.865469 0.720604 0.081306
# Oregon 0.506174 0.212421 0.061561
2. apply()
apply()是pandas里DataFrame的函数,可以针对DataFrame中的行数据或列数据应用操作。
注意:这里的apply是应用到每一行或者每一列操作,有专门的axis可以指定,默认是axis=0.
func = lambda x: x.max() - x.min()
print(frame.apply(func))
# 输出如下:
# a 0.851545
# b 0.707202
# c 0.662415
# dtype: float64
# --------------------------
df_im_data.loc[:,["from_user", "to_user"]].head()
# from_user to_user
# id
# 1094736 sso-1361713 tuliu001
# 1094737 tuliu001 sso-1361713
# 1094738 sso-1367729 sc-1131
# 1094739 sso-1250193 tuliu002
# 1094741 sso-1367729 tuliu002
def get_two_user_code(user1, user2):
s = [user1, user2]
l = sorted(s)
code = "@".join(l)
return code
c = df_im_data.loc[:,["from_user", "to_user"]].apply(lambda x: get_two_user_code(x[0], x[1]), axis=1)
# id
# 1094736 sso-1361713@tuliu001
# 1094737 sso-1361713@tuliu001
# 1094738 sc-1131@sso-1367729
# 1094739 sso-1250193@tuliu002
# 1094741 sso-1367729@tuliu002
# dtype: object
3. applymap()
apply()是pandas里DataFrame的函数,但是它对DataFrame中所有的元素应用操作。
func = lambda x: f'{x:.2f}%'
print(frame.applymap(func))
# 输出如下:
# a b c
# Utah 0.34% 0.43% 0.67%
# Ohio 0.75% 0.50% 0.14%
# Texas 0.68% 0.28% 0.90%
# Oregon 0.05% 0.86% 0.78%
4. map()
只能直接对序列元素的操作,所以必须对DataFrame的某列 (只针对于列) 应用操作。
func = lambda x: f'{x:.2f}%'
print(frame['a'].map(func))
# 输出如下:
# Utah 0.65%
# Ohio 0.90%
# Texas 0.09%
# Oregon 0.72%
# Name: a, dtype: object
标签:map,applymap,DataFrame,sso,user,func,apply
From: https://www.cnblogs.com/matrioc/p/17312467.html