if today's date <15"/>
  简体   繁体   中英

How to remove specific columns from multi-index based on date condition

I need to drop the subcolumns from multiindex dataframe based on today's date

df = pd.pivot_table(df, index=['PC', 'Geo', 'Comp'], values=['Bill1', 'Bill2', Bill3], 
                        columns=['Month'], fill_value=0)


dataframe before pivot table

在此处输入图片说明


desired output

please note that the output is based on current month
if today's date >15

今天的日期 >15

if today's date <15 今天的日期<15

You can modify your code as below.

df.iloc[:,np.in1d(df.columns.get_level_values(1), [dat])]

Hope this will solve your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM