Pandas中pivot的使用

 pivot函数用于从给定的表中创建出新的派生表,pivot有三个参数:索引、列和值。具体如下:

def pivot_simple(index, columns, values):
    """
    Produce 'pivot' table based on 3 columns of this DataFrame.
    Uses unique values from index / columns and fills with values.
    Parameters
    ----------
    index : ndarray
        Labels to use to make new frame's index
    columns : ndarray
        Labels to use to make new frame's columns
    values : ndarray
        Values to use for populating new frame's values

pivot行转列函数: df.pivot() 将长数据集转换成宽数据集,

import pandas as pd
# 读取week数据集
week = pd.read_csv('data/week.csv')
week
  Name Month Week Weight Perc Weight Loss
6 Bob Jan Week 4 283 -0.027
7 Amy Jan Week 4 190 -0.036
14 Bob Feb Week 4 268 -0.053
15 Amy Feb Week 4 173 -0.089
22 Bob Mar Week 4 261 -0.026
23 Amy Mar Week 4 170 -0.017
30 Bob Apr Week 4 250 -0.042
31 Amy Apr Week 4 161 -0.053
# 用pivot重构DataFrame,让Amy和Bob的数据并排放置
winner = week.pivot(index='Month', columns='Name', values='Perc Weight Loss')
winner
Name Amy Bob
Month    
Apr -0.053 -0.042
Feb -0.089 -0.053
Jan -0.036 -0.027
Mar -0.017 -0.026

猜你喜欢

转载自blog.csdn.net/weixin_48135624/article/details/114192982