|NO.Z.00045|——————————|BigDataEnd|——|Hadoop&Python.v09|——|Arithmetic.v09|Pandas数据分析库:Pandas数据重塑|

一、数据重塑
### --- 数据重塑

import numpy as np
import pandas as pd
df = pd.DataFrame(data = np.random.randint(0,100,size = (10,3)),
                  index = list('ABCDEFHIJK'),
                  columns=['Python','Tensorflow','Keras'])
df.T # 转置
df2 = pd.DataFrame(data = np.random.randint(0,100,size = (20,3)),
                   index = pd.MultiIndex.from_product([list('ABCDEFHIJK'),['期中','期末']]),#多层索引
columns=['Python','Tensorflow','Keras'])
df2.unstack(level = -1)                                             # ⾏旋转成列,level指定哪⼀层,进⾏变换
df2.stack()                                                         # 列旋转成⾏
df2.stack().unstack(level = 1)                                      # ⾏列互换

# 多层索引DataFrame数学计算
df2.mean()                                                          # 各学科平均分
df2.mean(level=0)                                                   # 各学科,每个⼈期中期末平均分
df2.mean(level = 1)                                                 # 各学科,期中期末所有⼈平均分

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

 

posted on 2022-04-16 14:44  yanqi_vip  阅读(28)  评论(0)    收藏  举报

导航