• <bdo id='fCKEH'></bdo><ul id='fCKEH'></ul>

      <legend id='fCKEH'><style id='fCKEH'><dir id='fCKEH'><q id='fCKEH'></q></dir></style></legend>
    1. <i id='fCKEH'><tr id='fCKEH'><dt id='fCKEH'><q id='fCKEH'><span id='fCKEH'><b id='fCKEH'><form id='fCKEH'><ins id='fCKEH'></ins><ul id='fCKEH'></ul><sub id='fCKEH'></sub></form><legend id='fCKEH'></legend><bdo id='fCKEH'><pre id='fCKEH'><center id='fCKEH'></center></pre></bdo></b><th id='fCKEH'></th></span></q></dt></tr></i><div id='fCKEH'><tfoot id='fCKEH'></tfoot><dl id='fCKEH'><fieldset id='fCKEH'></fieldset></dl></div>

      <small id='fCKEH'></small><noframes id='fCKEH'>

        <tfoot id='fCKEH'></tfoot>
      1. 将唯一值转换为列名的PANAS数据框

        Pandas dataframe to convert the unique value as column name(将唯一值转换为列名的PANAS数据框)
        <tfoot id='Y62tj'></tfoot>

          <legend id='Y62tj'><style id='Y62tj'><dir id='Y62tj'><q id='Y62tj'></q></dir></style></legend>

          <small id='Y62tj'></small><noframes id='Y62tj'>

            • <bdo id='Y62tj'></bdo><ul id='Y62tj'></ul>

                  <tbody id='Y62tj'></tbody>
                  <i id='Y62tj'><tr id='Y62tj'><dt id='Y62tj'><q id='Y62tj'><span id='Y62tj'><b id='Y62tj'><form id='Y62tj'><ins id='Y62tj'></ins><ul id='Y62tj'></ul><sub id='Y62tj'></sub></form><legend id='Y62tj'></legend><bdo id='Y62tj'><pre id='Y62tj'><center id='Y62tj'></center></pre></bdo></b><th id='Y62tj'></th></span></q></dt></tr></i><div id='Y62tj'><tfoot id='Y62tj'></tfoot><dl id='Y62tj'><fieldset id='Y62tj'></fieldset></dl></div>
                1. 本文介绍了将唯一值转换为列名的PANAS数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有下面提到格式的 pandas 数据帧

                  input_df :
                      gw_mac        mac          val    status 
                  0   AC233FC01403  AC233F264A4C -21    Outwards
                  1   AC233FC015F6  AC233F264A4C -37    Outwards
                  2   AC233FC01403  AC233F264A4C -20    Outwards
                  3   AC233FC015F6  AC233F264A4C -37    Outwards
                  4   AC233FC01403  AC233F264A4C -29    Outwards
                  5   AC233FC015F6  AC233F264A4C -39    Outwards
                  6   AC233FC01403  AC233F264A4C -37    Outwards
                  7   AC233FC015F6  AC233F264A4C -37    Outwards
                  8   AC233FC01403  AC233F264A4C -22    Outwards
                  9   AC233FC015F6  AC233F264A4C -37    Outwards
                  10  AC233FC015F6  AC233F264A4C -37    Outwards
                  

                  我需要转换与下面相同的,

                  output_df:
                      AC233FC01403  AC233FC015F6  mac            status    
                  1   -21           -37           AC233F264A4C   Outwards  
                  2   -20           -37           AC233F264A4C   Outwards
                  3   -29           -39           AC233F264A4C   Outwards
                  4   -37           -37           AC233F264A4C   Outwards
                  5   -22           -37           AC233F264A4C   Outwards
                  6    0            -37           AC233F264A4C   Outwards
                  

                  推荐答案

                  对新的counter列使用cumcountset_indexunstackreset_index

                  g = df.groupby(['gw_mac','mac','status']).cumcount()
                  
                  df = (df.set_index([g, 'mac','status','gw_mac'])['val']
                          .unstack(fill_value=0)
                          .reset_index(level=[1,2])
                          .rename_axis(None, axis=1))
                  print (df)
                              mac    status  AC233FC01403  AC233FC015F6
                  0  AC233F264A4C  Outwards           -21           -37
                  1  AC233F264A4C  Outwards           -20           -37
                  2  AC233F264A4C  Outwards           -29           -39
                  3  AC233F264A4C  Outwards           -37           -37
                  4  AC233F264A4C  Outwards           -22           -37
                  5  AC233F264A4C  Outwards             0           -37
                  

                  如果列顺序很重要:

                  df = df[df.columns[2:].tolist() + df.columns[:2].tolist()]
                  print (df)
                     AC233FC01403  AC233FC015F6           mac    status
                  0           -21           -37  AC233F264A4C  Outwards
                  1           -20           -37  AC233F264A4C  Outwards
                  2           -29           -39  AC233F264A4C  Outwards
                  3           -37           -37  AC233F264A4C  Outwards
                  4           -22           -37  AC233F264A4C  Outwards
                  5             0           -37  AC233F264A4C  Outwards
                  

                  这篇关于将唯一值转换为列名的PANAS数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)
                  Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)
                  Is there a way of group by month in Pandas starting at specific day number?( pandas 有从特定日期开始的按月分组的方式吗?)
                  Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)
                  Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)
                  Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)
                    <bdo id='Aisbs'></bdo><ul id='Aisbs'></ul>

                        <tbody id='Aisbs'></tbody>
                    1. <i id='Aisbs'><tr id='Aisbs'><dt id='Aisbs'><q id='Aisbs'><span id='Aisbs'><b id='Aisbs'><form id='Aisbs'><ins id='Aisbs'></ins><ul id='Aisbs'></ul><sub id='Aisbs'></sub></form><legend id='Aisbs'></legend><bdo id='Aisbs'><pre id='Aisbs'><center id='Aisbs'></center></pre></bdo></b><th id='Aisbs'></th></span></q></dt></tr></i><div id='Aisbs'><tfoot id='Aisbs'></tfoot><dl id='Aisbs'><fieldset id='Aisbs'></fieldset></dl></div>

                      <small id='Aisbs'></small><noframes id='Aisbs'>

                      <tfoot id='Aisbs'></tfoot>
                        <legend id='Aisbs'><style id='Aisbs'><dir id='Aisbs'><q id='Aisbs'></q></dir></style></legend>