【问题标题】:Is there a pandas function to convert all the column heads against a particular header row into rows for that particular header是否有一个 pandas 函数可以将针对特定标题行的所有列标题转换为该特定标题的行
【发布时间】:2020-06-23 11:04:03
【问题描述】:

我有一个像这样的 pandas 数据框,其中所有类别的头部数量都可用:

Table depicting category wise head wise values where the heads are in the columns

现在,我想要一个 pandas 函数返回如下图所示的数据框,其中每个类别的头部值将显示为行:

Table where all the heads for a particular category appears as a separate row instead of column

【问题讨论】:

    标签: python-3.x pandas dataframe rows


    【解决方案1】:

    您可以使用pd.melt() 将您的数据框从宽格式转换为长格式,其中'CATEGORY' 是您的标识符变量,所有其他列下的元素都是值并获得自己的行。

    new_df = df.melt(id_vars=['CATEGORY'], value_vars = ['ELCH_TOT','FXCH','FPA','ELDT','MTR_RENT','SOLAR_REBATE'])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-26
      • 1970-01-01
      • 1970-01-01
      • 2016-03-27
      • 2021-10-15
      相关资源
      最近更新 更多