J Westwood
J Westwood

Reputation: 431

drop columns in pandas dataframe based on mask

I have a dataframe with various number of values in each columns. I created a mask that tells me how many values in each column with the following code from another post > I get the following results

count_year_mask = df_mth_return.notnull().sum()
results in series like this
AAPL US Equity     312
GOOGL US Equity    161
GOOG US Equity      45
MSFT US Equity     312
AMZN US Equity     248
FB US Equity        68

I then want to delete all the columns in df_mth_return that are LESS THAN 180 from the above series. I want the DF to only have columns with > 180 numbers. So GOOGL, GOOG and FB would be eliminated. I tried this code and got the following error

df_mth_return.drop(np.where(count_year_mask<180))
ValueError: Buffer has wrong number of dimensions (expected 1, got 3)

This seems like a simple mask so not sure what I am doing wrong. Please help if you can

Upvotes: 4

Views: 6696

Answers (1)

jezrael
jezrael

Reputation: 862751

You can filter columns with loc:

df_mth_return.loc[:, count_year_mask>=180]

Or:

df_mth_return.loc[:, ~count_year_mask<180]

Upvotes: 4

Related Questions