- 第⼋部分 数据转换
- 2、替换值
df.replace(3,1024) # 将 3 替 换 为 1024 df.replace([0,7],2048) # 将 0 和 7 替 换 为 2048 df.replace({0:512,np.nan:998}) # 根据字典键值对进⾏替换
df.replace({‘Python’:2},-1024) # 将Python这⼀列中等于2的,替换为-1024 - 1、map批量元素改变,Series专有df[‘Keras’].map({1:’Hello’,5:’World’,7:’AI’}) # 字典映射df[‘Python’].map(lambda x:True if x >=5 else False) # 隐式函数映射def convert(x): # 显示函数映射
if x%3 == 0:
return True elif x%3 == 1:
return False df[‘Tensorflow’].map(convert) - 2、applymap DataFrame专有
df.applymap(lambda x : x + 100) # 计算DataFrame中每个元素 - 1、⼀列执⾏多项计算df[‘Python’].transform([np.sqrt,np.exp]) # Series处理def convert(x):
if x.mean() > 5: x = 10
else:
x = -10 return x
# 2、多列执⾏不同计算
df.transform({‘Python’:convert,’Tensorflow’:np.max,’Keras’:np.min}) # DataFrame
处理 - 哑变量,独热编码,1表示有,0表示没有
df = pd.DataFrame({‘key’:[‘b’,’b’,’a’,’c’,’a’,’b’]}) pd.get_dummies(df,prefix=’’,prefix_sep=’’)
第⼋部分 数据转换
第⼀节 轴和元素替换
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’])
df.iloc[4,2] = None # 空数据
#1、重命名轴索引
df.rename(index = {‘A’:’AA’,’B’:’BB’},columns = {‘Python’:’⼈⼯智能’})
2、替换值
df.replace(3,1024) # 将 3 替 换 为 1024 df.replace([0,7],2048) # 将 0 和 7 替 换 为 2048 df.replace({0:512,np.nan:998}) # 根据字典键值对进⾏替换
df.replace({‘Python’:2},-1024) # 将Python这⼀列中等于2的,替换为-1024
第⼆节 map Series元素改变
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’])
df.iloc[4,2] = None # 空数据
1、map批量元素改变,Series专有df[‘Keras’].map({1:’Hello’,5:’World’,7:’AI’}) # 字典映射df[‘Python’].map(lambda x:True if x >=5 else False) # 隐式函数映射def convert(x): # 显示函数映射
if x%3 == 0:
return True elif x%3 == 1:
return False df[‘Tensorflow’].map(convert)
第三节 apply元素改变。既⽀持 Series,也⽀持 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’])
df.iloc[4,2] = None # 空数据
# 1、apply 应⽤⽅法数据转换,通⽤
# Series,其中x是Series中元素
df[‘Keras’].apply(lambda x:True if x >5 else False) # DataFrame,其中的x是DataFrame中列或者⾏,是Series df.apply(lambda x : x.median(),axis = 0) # 列的中位数def convert(x): # ⾃定义⽅法
return (x.mean().round(1),x.count()) df.apply(convert,axis = 1) # ⾏平均值,计数
2、applymap DataFrame专有
df.applymap(lambda x : x + 100) # 计算DataFrame中每个元素
第四节 transform变形⾦刚
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’])
df.iloc[4,2] = None # 空数据
1、⼀列执⾏多项计算df[‘Python’].transform([np.sqrt,np.exp]) # Series处理def convert(x):
if x.mean() > 5: x = 10
else:
x = -10 return x
# 2、多列执⾏不同计算
df.transform({‘Python’:convert,’Tensorflow’:np.max,’Keras’:np.min}) # 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’])
ran = np.random.permutation(10) # 随机重排df.take(ran) # 重 排 DataFrame df.take(np.random.randint(0,10,size = 15)) # 随机抽样