-->

How to remove specific columns from multi-index ba

2020-07-30 03:22发布

问题:

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

if today's date <15

回答1:

You can modify your code as below.

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

Hope this will solve your problem.