强大的apply函数
Dataframe的apply函数默认作用于Dataframe的各列.
DataFrame.apply(func, axis=0, broadcast=False, raw=False, reduce=None, args=(), **kwds)
配合apply函数使用的qcut()函数
pandas的qcut可以把一组数字按大小区间进行分区,比如
data = pd.Series([0,8,1,5,3,7,2,6,10,4,9])
比如我要把这组数据分成两部分,一半大的,一半小的,如果是小的数,值就变成’small number’,大的数,值就变成’large number’:
print(pd.qcut(data,[0,0.5,1],labels=['small number','large number']))
small numbers
large numbers
small numbers
small numbers
small numbers
large numbers
small numbers
large numbers
large numbers
small numbers
large numbers
dtype: category
Categories (2, object): [small numbers < large numbers]
apply()函数对每列数据标准化
def Standardization(col): # 将数据简单标准化
return (col - col.min()) / (col.max() - col.min())
df_S_Stander = df_S.iloc[:, 0:-1].apply(Standardization) # 将任务完成的简单规范化
df_F_Stander = df_F.iloc[:, 0:-1].apply(Standardization) # 将任务没完成的简单规范化
df_test.apply(lambda x: (x - np.min(x)) / (np.max(x) - np.min(x)))