【问题标题】:Need help turning rows in columns using python Pandas需要帮助使用 python Pandas 在列中转换行
【发布时间】:2020-07-27 18:02:39
【问题描述】:

我有一个目前看起来像这样的数据集:

 FCST/BUD/ACT   Vendor  Act GL  Acct    Manager Amount
 FCST          Walmart   962.0  56340.0 Kunal  1234567
 FCST          Walmart   962.0  56340.0 Kunal  1234567
 ACT           Walmart   962.0  56340.0 Kunal  1234567
 ACT           Kohls    962.0   56340.0 Kunal   1234567
 FCST          TJ      4521.0   56340.0 Labeeb  1234567
 BUD           TJ     4521.0    56340.0 Labeeb 1234567

我需要在自己的单独列中制作 FCST/BUD/ACT 我希望数据看起来像这样

               Vendor   Act GL  Acct    Manager FCST    BUD   ACT
                Walmart   962.0 56340.0 Kunal  5555     5555
                Walmart   962.0 56340.0 Kunal  567      3200
               Walmart   962.0  56340.0 Kunal  1234     500       6160
               Kohls    962.0   56340.0 Kunal  2354     321   569
               TJ      4521.0   56340.0 Labeeb  1234567
               TJ     4521.0    56340.0 Labeeb 1234567

我试过了:

data_teams=pd.pivot_table(data,index=['Act GL','Vendor','Acct','SLT +1','Teams','Account'],columns='FCST/BUD/ACT',values=['total year Amount'],fill_value='0').reset_index()

但这不起作用,因为它没有给我所有的供应商名称(随机空格),因为不同的经理重复了一些值。此外,由于我需要表格数据,因此数据透视表格式并不理想。除了数据透视表还有其他方法吗?

【问题讨论】:

  • 能不能加个命令生成原始数据框
  • 顺便说一句,您从哪里获得第一行的FCST 列中的值5555

标签: python pandas pivot-table data-wrangling


【解决方案1】:

似乎groupbyunstack 是这里的最佳选择:

df = pd.DataFrame({'foo': ['one', 'one', 'three', 'two', 'two',
                           'four'],
                   'bar': ['A', 'B', 'C', 'A', 'B', 'C'],
                   'baz': [1, 2, 3, 4, 5, 6],
                   'zoo': ['x', 'y', 'z', 'q', 'w', 't']})

col = 'foo'
value_col = 'baz'
level = len(df.columns) - 2
use_cols = [i for i in df.columns if i not in [col, value_col]]+[col]
_df = df.groupby(use_cols)[value_col].agg(lambda x:x).unstack(level=level).reset_index()
_df.columns.name = ''

输入:

     foo bar  baz zoo
0    one   A    1   x
1    one   B    2   y
2  three   C    3   z
3    two   A    4   q
4    two   B    5   w
5   four   C    6   t

输出:

  bar zoo  four  one  three  two
0   A   q   NaN  NaN    NaN  4.0
1   A   x   NaN  1.0    NaN  NaN
2   B   w   NaN  NaN    NaN  5.0
3   B   y   NaN  2.0    NaN  NaN
4   C   t   6.0  NaN    NaN  NaN
5   C   z   NaN  NaN    3.0  NaN

随意fillna

【讨论】:

    【解决方案2】:

    @Partha Mandal 使用 groupby 和 unstack 来执行分析。这是另一种方式,使用 pivot_table:

    首先,创建数据框。 reset_index() 函数创建一列顺序整数,因此我们将获得每行输入对应的一行输出。

    columns = ['FCST/BUD/ACT', 'Vendor', 'Act', 'GL', 'Acct_Manager', 'Amount']
    
    data = [
        ('FCST', 'Walmart', 962.0, 56340.0, 'Kunal', 1234567), 
        ('FCST', 'Walmart', 962.0, 56340.0, 'Kunal', 1234567), 
        ('ACT', 'Walmart', 962.0, 56340.0, 'Kunal', 1234567), 
        ('ACT', 'Kohls', 962.0, 56340.0, 'Kunal', 1234567), 
        ('FCST', 'TJ', 4521.0, 56340.0, 'Labeeb', 1234567), 
        ('BUD', 'TJ', 4521.0, 56340.0, 'Labeeb', 1234567)]
    
    df = pd.DataFrame(data, columns=columns).reset_index()
    

    其次,调用 pivot_table(),并删除我们在上一步中创建的索引(整数)列。还要修改列索引的名称。

    df_new = df.pivot_table(index=['index', 'Vendor', 'Act', 'GL', 'Acct_Manager'],
                            columns='FCST/BUD/ACT',
                            values='Amount',
                            aggfunc='sum',
                            fill_value=0
                           ).reset_index().drop(columns='index')
    
    df_new.columns.name = ''
    

    最后,根据原帖中的数据展示结果:

    print(df_new)
    
        Vendor     Act       GL Acct_Manager      ACT      BUD     FCST
    0  Walmart   962.0  56340.0        Kunal        0        0  1234567
    1  Walmart   962.0  56340.0        Kunal        0        0  1234567
    2  Walmart   962.0  56340.0        Kunal  1234567        0        0
    3    Kohls   962.0  56340.0        Kunal  1234567        0        0
    4       TJ  4521.0  56340.0       Labeeb        0        0  1234567
    5       TJ  4521.0  56340.0       Labeeb        0  1234567        0
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-26
      • 2011-03-26
      相关资源
      最近更新 更多