zoukankan      html  css  js  c++  java
  • pd.melt详解--列转行

    方法详解:

    pandas.melt(frame, id_vars=None, value_vars=None, var_name=None, value_name='value', col_level=None

    “Unpivots” a DataFrame from wide format to long format, optionally leaving identifier variables set.

    如何理解Unpivots其实就是列转行。

    This function is useful to massage a DataFrame into a format where one or more columns are identifier variables (id_vars)
    , while all other columns, considered measured variables (value_vars)
    , are “unpivoted” to the row axis, leaving just two non-identifier columns, ‘variable’ and ‘value’.
    frame DataFrame

    id_vars : tuple, list, or ndarray, optional

      Column(s) to use as identifier variables.其实就是作为主键

    value_vars : tuple, list, or ndarray, optional

      Column(s) to unpivot. If not specified, uses all columns that are not set as id_vars.对那些字段进行转列且返回数据只有该字段的数据。

    var_name : scalar

      Name to use for the ‘variable’ column. If None it uses frame.columns.name or ‘variable’.对行专列的后,多个列名组成的列用什么名字?默认用variable

    value_name : scalar, default ‘value’.

      Name to use for the ‘value’ column.对行专列的后,多个列名下的值组成的列用什么名字?默认用value

    col_level : int or string, optional

      If columns are a MultiIndex then use this level to melt.

    其实这个和我之前数仓博客hive数据仓库表设计之(矮宽表+高窄表)异曲同工:

    官网案例:

    import pandas as pd
    df = pd.DataFrame({'A': {0: 'a', 1: 'b', 2: 'c'},
                'B': {0: 1, 1: 3, 2: 5},
                       'C': {0: 2, 1: 4, 2: 6}})
    df

    pd.melt(df, id_vars=['A'], value_vars=['B'])

    pd.melt(df, id_vars=['A'], value_vars=['B','C'])

  • 相关阅读:
    VMware安装Centos7超详细过程(图文)
    linux中yum与rpm区别
    xshell连接本地虚拟机中的centos
    虚拟主机安装 CentOS 8 出现 “ pane is dead ” 故障解决方案
    python字符串前面添加(u,r,b)的功能
    from . import XXX
    我关注的博客
    并发与并行的区别
    GSAP学习(二)——载入
    GSAP学习(一)——什么是GSAP
  • 原文地址:https://www.cnblogs.com/wqbin/p/12782058.html
Copyright © 2011-2022 走看看