Reputation: 1010
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?
Upvotes: 3
Views: 4969
Reputation: 294218
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
Upvotes: 0
Reputation: 862481
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
Upvotes: 5