是否有 pandas 函数可以将特定 header 行的所有列标题转换为该特定 header 的行
Is there a pandas function to convert all the column heads against a particular header row into rows for that particular header
我有一个像这样的 pandas 数据框,其中所有类别的头部数量都可用:
Table depicting category wise head wise values where the heads are in the columns
现在,我想要一个 pandas 函数来 return 数据框,如下图所示,其中头部明智值将显示为每个类别的行:
Table where all the heads for a particular category appears as a separate row instead of column
您可以使用 pd.melt()
将数据框从宽格式转换为长格式,其中 'CATEGORY'
是您的标识符变量,所有其他列下的元素都是值并有自己的行.
new_df = df.melt(id_vars=['CATEGORY'], value_vars = ['ELCH_TOT','FXCH','FPA','ELDT','MTR_RENT','SOLAR_REBATE'])
我有一个像这样的 pandas 数据框,其中所有类别的头部数量都可用:
Table depicting category wise head wise values where the heads are in the columns
现在,我想要一个 pandas 函数来 return 数据框,如下图所示,其中头部明智值将显示为每个类别的行:
Table where all the heads for a particular category appears as a separate row instead of column
您可以使用 pd.melt()
将数据框从宽格式转换为长格式,其中 'CATEGORY'
是您的标识符变量,所有其他列下的元素都是值并有自己的行.
new_df = df.melt(id_vars=['CATEGORY'], value_vars = ['ELCH_TOT','FXCH','FPA','ELDT','MTR_RENT','SOLAR_REBATE'])