自定义函数
目录自定义操作map()
map,用于Series实现每个值得处理
map() 是一个Series的函数,map()将一个自定义函数应用于Series结构中的每个元素(elements)
无法实现对列行的整体统计
import pandas as pd
df = pd.DataFrame({'key1' : ['a', 'a', 'b', 'b', 'a'],
'key2' : ['one', 'two', 'one', 'two', 'one'],
'data1' : np.arange(5),
'data2' : np.arange(5,10)})
print(df)
# key1 key2 data1 data2
# 0 a one 0 5
# 1 a two 1 6
# 2 b one 2 7
# 3 b two 3 8
# 4 a one 4 9
#lambda在这里其实是在定义一个简单的函数,一个没有函数名的函数。
df['data1'] = df['data1'].map(lambda x : "%.3f"%x)
print(df)
# key1 key2 data1 data2
# 0 a one 0.000 5
# 1 a two 1.000 6
# 2 b one 2.000 7
# 3 b two 3.000 8
# 4 a one 4.000 9
自定义操作apply()
apply() 函数在DataFrame和Series对象中都可以使用。它将一个函数应用到DataFrame或Series的每一个元素。如果在DataFrame中使用,函数应该按照列或行(取决于axis参数)的元素进行操作
apply()是Pandas DataFrame和Series对象的一个方法,它允许你在DataFrame或Series的每一个元素上应用一个函数
Series.apply(func) 函数参数是每个值
DataFrame.apply(func) 函数参数是Series
DataFrame.apply(func, axis=0, broadcast=None, raw=False, reduce=None, result_type=None, args=(), **kwds)
func : function作用于每一列或行
axis 函数所应用的轴 0,1,默认为0
broadcast 可选,仅与聚合函数相关
raw
reduce
返回值
applied : Series 或 DataFrame
对指定轴的操作,聚合,统计
import pandas as pd
data = {
'A': [1, 2, 3],
'B': [4, 5, 6],
'C': [7, 8, 9]
}
df = pd.DataFrame(data)
#计算每一列的平均值
result = df.apply(lambda x: x.mean())
print(result)
#A 2.0
#B 5.0
#C 8.0
#dtype: float64
# 计算每一行的最大值
result = df.apply(lambda x: x.max(), axis=1)
print(result)
#0 7
#1 8
#2 9
#dtype: int64
对每个数值进行转换
import pandas as pd
data = {
'A': [1, 2, 3],
'B': [4, 5, 6],
'C': [7, 8, 9]
}
df = pd.DataFrame(data)
# 计算每一个数值的平方
result = df.apply(lambda x: x*x)
print(result)
# A B C
# 0 1 16 49
# 1 4 25 64
# 2 9 36 81
#对每一列进行归一化,使得每一列的最小值为 0,最大值为 1
result = df.apply(lambda x: (x - x.min()) / (x.max() - x.min()))
print(result)
# A B C
#0 0.0 0.0 0.0
#1 0.5 0.5 0.5
#2 1.0 1.0 1.0
def subtract_custom_value(x, custom_value):
return x - custom_value
result=df.apply(subtract_custom_value, args=(5,))
print(result)
# A B C
# 0 -4 -1 2
# 1 -3 0 3
# 2 -2 1 4
import pandas as pd
import numpy as np
df = pd.DataFrame({'key1' : ['a', 'a', 'b', 'b', 'a'],
'key2' : ['one', 'two', 'one', 'two', 'one'],
'data1' : np.arange(5),
'data2' : np.arange(5,10)})
print(df)
# key1 key2 data1 data2
# 0 a one 0 5
# 1 a two 1 6
# 2 b one 2 7
# 3 b two 3 8
# 4 a one 4 9
df.loc['total'] = df[['data1','data2']].apply(lambda x : x.sum(),axis=0)
print(df)
# key1 key2 data1 data2
# 0 a one 0.0 5.0
# 1 a two 1.0 6.0
# 2 b one 2.0 7.0
# 3 b two 3.0 8.0
# 4 a one 4.0 9.0
# total NaN NaN 10.0 35.0
def stradd(x):
x=copy.deepcopy(x)
x=x.tolist()
x=[str(i) for i in x]
return "".join(x)
df['data3'] = df.apply(stradd,axis=1)
print(df)
# key1 key2 data1 data2 data3
# 0 a one 0 5 aone05
# 1 a two 1 6 atwo16
# 2 b one 2 7 bone27
# 3 b two 3 8 btwo38
# 4 a one 4 9 aone49
自定义操作applymap()
只用于DataFrame,处理数据的每个元素
applymap()函数是Pandas DataFrame对象的一个方法,它允许你在DataFrame的每一个元素上应用一个函数,就像apply()一样。
但是,applymap()还会保留输入的DataFrame的结构,即它的轴标签和索引
import pandas as pd
import numpy as np
df = pd.DataFrame({'key1' : ['a', 'a', 'b', 'b', 'a'],
'key2' : ['one', 'two', 'one', 'two', 'one'],
'data1' : np.arange(5),
'data2' : np.arange(5,10)})
print(df)
# key1 key2 data1 data2
# 0 a one 0 5
# 1 a two 1 6
# 2 b one 2 7
# 3 b two 3 8
# 4 a one 4 9
def addA(x):
return "A" + str(x )
df.applymap(addA)
print(df)
# key1 key2 data1 data2
# 0 Aa Aone A0.0 A5.0
# 1 Aa Atwo A1.0 A6.0
# 2 Ab Aone A2.0 A7.0
# 3 Ab Atwo A3.0 A8.0
# 4 Aa Aone A4.0 A9.0
# total Anan Anan A10.0 A35.0
参考资料
https://blog.csdn.net/maymay_/article/details/80229053
https://www.cnblogs.com/BlairGrowing/p/15866516.html
https://www.cjavapy.com/article/303/
标签:映射,自定义,df,two,DataFrame,print,apply,data1,pandas From: https://www.cnblogs.com/tian777/p/17706396.html