2.3.3 apply元素改变,既支持 Series也支持 DataFrame
2.3.3.1 apply简单介绍
创造数据:
import numpy as np import pandas as pd df = pd.DataFrame(np.random.randint(0, 101, size = (30, 3)), columns = ['Python', 'Math', 'English']) display(df)
apply 对数据进行修改:
def convert(x): if x < 60: return '不及格' elif x < 80: return '中等' else: return '优秀' df['Python'].apply(convert)
我们现在来把这两个表格进行合并:
res = df['Python'].apply(convert) index = list(df.columns).index('Python') + 1 df.insert(loc = index, column = 'Python' + '等级', value = res) df
如果我们要生成所有学科的等级,我们可以利用 for 循环:
def convert(x): if x < 60: return '不及格' elif x < 80: return '中等' else: return '优秀' for col in list(df.columns): res = df[col].apply(convert) index = list(df.columns).index(col) + 1 df.insert(loc = index, column = col + '等级', value = res) df
2.3.3.2 apply应用
apply 可以对一列数据进行修改:
import numpy as np import pandas as pd df = pd.DataFrame(data = np.random.randint(0, 10, size = (10, 3)), index = list('ABCDEFHIJK'), columns = ['Python', 'Tensorflow', 'Keras']) display(df) # apply 应用,方法:自定义、简单隐式函数(lambda) df['Python'] = df['Python'].apply(lambda x : x + 100) display(df)
map 可以对一列数据进行修改:
import numpy as np import pandas as pd df = pd.DataFrame(data = np.random.randint(0, 10, size = (10, 3)), index = list('ABCDEFHIJK'), columns = ['Python', 'Tensorflow', 'Keras']) display(df) df['Python'] = df['Python'].map(lambda x : x - 100) display(df)
2.3.3.3 applymap应用
applymap 可以对整个 DataFrame 进行全部的处理
import numpy as np import pandas as pd df = pd.DataFrame(data = np.random.randint(0, 10, size = (10, 3)), index = list('ABCDEFHIJK'), columns = ['Python', 'Tensorflow', 'Keras']) display(df) def convert(x): if x < 5: return 100 else: return -100 df.applymap(convert)