DataFrame的applymap
将DataFrame的每个元素经过函数运算之后转化成新的元素
import pandas as pd
df = pd.DataFrame({
'a':[1,2,3],
'b':[4,5,6]
},index=['L1','L2','L3'])
print(df)
df1 = df.applymap(lambda x:'even' if x%2==0 else 'odd')
print(df1)
#使用如下代码对DataFrame进行转换,如果元素的值大于等于50,将其转换为A,如果大于等于30且小于50,转换为B,如果小于30,转换为C
df = pd.DataFrame({
's1': [27.93, 58.08, 38.67, 45.83, 70.26, 46.61, 49.73, 34.02, 56.64, 57.28],
's2': [28.18, 50.61, 31.73, 31.48, 55.96, 22.73, 40.47, 42.02, 31.39, 64.21],
's3': [29.39, 51.62, 57.91, 45.94, 53.81, 45.77, 69.13, 28.75, 43.43, 55.7],
's4': [40.52, 48.55, 59.24, 71.21, 58.48, 63.63, 55.16, 34.9, 54, 68.03],
's5': [26.26, 54.03, 49.08, 46.53, 43.23, 56.79, 58.71, 26.43, 44.97, 54.16]
}, index = ['05-21', '05-22', '05-23', '05-24', '05-25', '05-26', '05-27', '05-28', '05-29', '05-30'])
def level_convert(amount):
if amount >= 50:
return 'A'
elif amount >= 30 and amount < 50:
return 'B'
else:
return 'C'
print(df.applymap(level_convert))
DataFrame apply的作用
1.将DataFrame的每行或者每列(Series)经过函数运算之后转化成新的行或列
2.将DataFrame的每行或者每列(Series)经过函数运算之后转换成一个值
import pandas as pd
df = pd.DataFrame([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
def to_vec(values):
return values>5
def to_value(values):
return values.sum()
print('转成新元素')
print(df.apply(to_vec))
print('转成新元素')
print(df)
print(df.apply(to_value,axis=1))
#使用DataFrame存储了5家水果店(名字为s1 ~ s5)在10天中的销售情况, # 完成如下代码填空,完成水果店评级操作:对水果店销量矩阵进行转换, # 当某个水果店销量大于等于当天销量均值时,为该店当天评级为A; # 当销量小于当天均值时,为该店当天评级为B。
#使用DataFrame存储了5家水果店(名字为s1 ~ s5)在10天中的销售情况,
# 完成如下代码填空,完成水果店评级操作:对水果店销量矩阵进行转换,
# 当某个水果店销量大于等于当天销量均值时,为该店当天评级为A;
# 当销量小于当天均值时,为该店当天评级为B。
import pandas as pd
df = pd.DataFrame({
's1': [27.93, 58.08, 38.67, 45.83, 70.26, 46.61, 49.73, 34.02, 56.64, 57.28],
's2': [28.18, 50.61, 31.73, 31.48, 55.96, 22.73, 40.47, 42.02, 31.39, 64.21],
's3': [29.39, 51.62, 57.91, 45.94, 53.81, 45.77, 69.13, 28.75, 43.43, 55.7],
's4': [40.52, 48.55, 59.24, 71.21, 58.48, 63.63, 55.16, 34.9, 54, 68.03],
's5': [26.26, 54.03, 49.08, 46.53, 43.23, 56.79, 58.71, 26.43, 44.97, 54.16]
}, index = ['05-21', '05-22', '05-23', '05-24', '05-25', '05-26', '05-27', '05-28', '05-29', '05-30'])
def cell_larger_than_mean(bool_v):
if bool_v:
return 'A'
else:
return 'B'
def larger_than_mean(numbers):
mean = numbers.mean()
return numbers > mean
print(df.apply(larger_than_mean, axis = 1).applymap(cell_larger_than_mean))
#使用DataFrame存储了5家水果店(名字为s1 ~ s5)在10天中的销售情况, # 完成如下代码填空,将水果店销量矩阵转换成每天销量最大的水果店与销量最小的水果店之间的差值, # 输出每天的字符串。
#使用DataFrame存储了5家水果店(名字为s1 ~ s5)在10天中的销售情况,
# 完成如下代码填空,将水果店销量矩阵转换成每天销量最大的水果店与销量最小的水果店之间的差值,
# 输出每天的字符串。
df = pd.DataFrame({
's1': [27.93, 58.08, 38.67, 45.83, 70.26, 46.61, 49.73, 34.02, 56.64, 57.28],
's2': [28.18, 50.61, 31.73, 31.48, 55.96, 22.73, 40.47, 42.02, 31.39, 64.21],
's3': [29.39, 51.62, 57.91, 45.94, 53.81, 45.77, 69.13, 28.75, 43.43, 55.7],
's4': [40.52, 48.55, 59.24, 71.21, 58.48, 63.63, 55.16, 34.9, 54, 68.03],
's5': [26.26, 54.03, 49.08, 46.53, 43.23, 56.79, 58.71, 26.43, 44.97, 54.16]
}, index = ['05-21', '05-22', '05-23', '05-24', '05-25', '05-26', '05-27', '05-28', '05-29', '05-30'])
print(df)
def gap_between_max_min(numbers):
return numbers.idxmax() + '-' + numbers.idxmin() + '=' + str(numbers.max() - numbers.min())[:4]
print(df.apply(gap_between_max_min, axis = 1))