3

I have the following dataframe

 2001-01-01   2001-01-02            2001-01-03 
   1               0                   8 

I want to drop every column, which is smaller than 2001-01-02, i.e. my df should look like this:

  2001-01-02            2001-01-03
          0                   8 

Does anybody know how to do it?

2 Answers 2

5

Use 'inverse' condition < to >=, because need only values equal or higher:

df = pd.DataFrame([[1,0,8]], columns = pd.date_range('2001-01-01', periods=3))
print (df)
   2001-01-01  2001-01-02  2001-01-03
0           1           0           8

print (df.columns >= '2001-01-02')
[False  True  True]


df1 = df.loc[:, df.columns >= '2001-01-02']
print (df1)
   2001-01-02  2001-01-03
0           0           8

cols = df.columns[df.columns >= '2001-01-02']
df1 = df[cols]
print (df1)
   2001-01-02  2001-01-03
0           0           8

Another solution is add ~ for inverse boolean array:

df1 = df.loc[:, ~(df.columns < '2001-01-02')]
print (df1)
   2001-01-02  2001-01-03
0           0           8
Sign up to request clarification or add additional context in comments.

Comments

0

You can use label slices with loc

df.loc[:, :'2001-01-02']

   2001-01-01  2001-01-02
0           1           0

And

df.loc[:, '2001-01-02':]

   2001-01-02  2001-01-03
0           0           8

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.