【问题标题】:How to drop VALUES column of pivot table dataframe如何删除数据透视表数据框的 VALUES 列
【发布时间】:2020-01-07 00:56:25
【问题描述】:

需要删除从数据透视表创建的多索引数据框的子列

只需要在特定列(月)动态删除子列

我有一个从数据透视表创建的数据框,需要动态删除特定列的子列...
如果今天的日期小于 15,我需要删除除 Sep-19 以外所有月份的子栏 Bill1(当月)
如果今天的日期大于 15,则应删除除 Oct-19(下个月)

data_frame1 = pd.pivot_table(data_frame, index=['PC', 'Geo', 'Comp'], values=['Bill1', 'Bill2'], columns=['Month'], fill_value=0)
data_frame1 = data_frame1.swaplevel(0,1, axis=1).sort_index(axis=1)
tuples = [(a.strftime('%b-%y'), b) if a!= 'All' else (a,b) for a,b in data_frame1.columns]
data_frame1.columns = pd.MultiIndex.from_tuples(tuples)

输出:

              Sep-19             OCT-19        Nov-19
             Bill1 Bill2      Bill1 Bill2     Bill1 Bill2     
PC Geo Comp
A  Ind   OS   1     1.28        1    1.28      1    1.28

所需的输出:
如果今天的日期小于 15

               Sep-19          OCT-19        Nov-19
              Bill1  Bill2       Bill2         Bill2     
PC Geo Comp
A  Ind   OS    1      1.28        1.28          1.28


如果今天的日期大于 15

               Sep-19       OCT-19            Nov-19
                Bill2     Bill1  Bill2        Bill2     
PC Geo Comp
A  Ind   OS     1.28        1     1.28         1.28

【问题讨论】:

  • @jezrael.. 兄弟如何删除子列 Bill2 以及 bill1

标签: python-3.x pandas dataframe multi-index


【解决方案1】:

用途:

#convert first level for datetimes and to month periods
level0 = pd.to_datetime(df.columns.get_level_values(0), format='%b-%y').to_period('m')
#get second level
level1 = df.columns.get_level_values(1)
print (level0)
PeriodIndex(['2019-09', '2019-09', '2019-10', '2019-10', '2019-11', '2019-11'],
             dtype='period[M]', freq='M')

print (level1)
Index(['Bill1', 'Bill2', 'Bill1', 'Bill2', 'Bill1', 'Bill2'], dtype='object')

#test for next 15 days
#dat = pd.to_datetime('2019-09-20')
#get today timestamp
dat = pd.to_datetime('now')
print (dat)

#convert timestamp to period
today_per = dat.to_period('m')

#compare day and filter
if dat.day < 15:
    df = df.loc[:, (level0 == today_per) | (level1 != 'Bill1')]
else:
    #test with add 1 month to today period
    df = df.loc[:, (level0 == today_per + 1) | (level1 != 'Bill1')]
print (df)
         Sep-19       Oct-19 Nov-19
          Bill1 Bill2  Bill2  Bill2
A Ind OS      1  1.28   1.28   1.28

下个月测试:

#convert first level for datetimes and to month periods
level0 = pd.to_datetime(df.columns.get_level_values(0), format='%b-%y').to_period('m')
#get second level
level1 = df.columns.get_level_values(1)
print (level0)
PeriodIndex(['2019-09', '2019-09', '2019-10', '2019-10', '2019-11', '2019-11'],
             dtype='period[M]', freq='M')

print (level1)
Index(['Bill1', 'Bill2', 'Bill1', 'Bill2', 'Bill1', 'Bill2'], dtype='object')

#test for next 15 days
dat = pd.to_datetime('2019-09-20')
#get today timestamp
#dat = pd.to_datetime('now')
print (dat)

#convert timestamp to period
today_per = dat.to_period('m')

#compare day and filter
if dat.day < 15:
    df = df.loc[:, (level0 == today_per) | (level1 != 'Bill1')]
else:
    #test with add 1 month to today period
    df = df.loc[:, (level0 == today_per + 1) | (level1 != 'Bill1')]
print (df)
         Sep-19 Oct-19       Nov-19
          Bill2  Bill1 Bill2  Bill2
A Ind OS   1.28      1  1.28   1.28

【讨论】:

  • 兄弟如何删除子列 Bill2 以及 bill1
  • 不,解决方案正在运行..谢谢您的回答..如果我需要删除 Bill2Bill1 列,而不仅仅是 Bill2 列怎么办? b>账单1
  • @PraveenSnowy - 未测试,但如果将 | (level1 != 'Bill1') 更改为 | (level1 != 'Bill1') | (level1 != 'Bill2') 它应该可以工作
  • @PraveenSnowy - 你能测试一下df = df.loc[:, (level0 != today_per) &amp; level1.isin(['Bill1', 'Bill2'])] 吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-02
  • 2011-09-07
  • 1970-01-01
  • 2020-11-18
  • 1970-01-01
  • 1970-01-01
  • 2017-12-07
相关资源
最近更新 更多