简体   繁体   中英

How do I select columns while having few conditions in pandas

I've got datframe:

   1990   1991   1992  ....  2015   2016   2017
0  9       40    300         100    200     554
1  9       70    700         3300   200    554
2  5       70    900         100    200     554
3  8       80    900         176    200     554
4  7       50    200         250    280     145
5  9       30    900         100    207     554
6  2       80    700         180    200     554
7  2       80    400         100    200     554
8  5       80    300         100    200     554
9  7       70    800         100    200     554

How do I select df<2000 & df>2005?

I tried code below but it failed:

1. df[(df.loc[:, :2000]) & (df.loc[:, 2005:])]
2. df[(df <2000) & (df>2005)]

Compare columns names:

print (df)
   1999  2002  2003  2005  2006  2017
0     9    40   300   100   200   554
1     9    70   700  3300   200   554
2     5    70   900   100   200   554
3     8    80   900   176   200   554
4     7    50   200   250   280   145
5     9    30   900   100   207   554
6     2    80   700   180   200   554
7     2    80   400   100   200   554
8     5    80   300   100   200   554
9     7    70   800   100   200   554


df = df.loc[:, (df.columns <2000) | (df.columns>2005)]
print (df)
   1999  2006  2017
0     9   200   554
1     9   200   554
2     5   200   554
3     8   200   554
4     7   280   145
5     9   207   554
6     2   200   554
7     2   200   554
8     5   200   554
9     7   200   554

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM