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, ie my df should look like this:
2001-01-02 2001-01-03
0 8
Does anybody know how to do it?
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
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
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.