1

as i have the data frame as follow

In [107]: xx
Out[107]:
   1         2         3         4
0  0 -1.234881  0.039231 -0.399870
1  1 -1.761733 -1.186537  0.043678
2  2  0.707564 -0.270639 -0.251519
3  3 -0.979584  0.476025 -1.587889
4  4 -0.576429  1.987681 -0.322581
5  5 -0.695509  1.285029  0.393906
6  6 -0.036627 -0.380702 -0.170813
7  7  0.673423  0.860289 -0.774651
8  8 -1.000333  0.978760  0.256645
9  9 -0.446005 -0.584627  0.187244

and the condition is the value of column = 1 just as

con = [2,4,6,8]

is there any function I can use, the I can get the result like follow:

   1         2         3         4
2  2  0.707564 -0.270639 -0.251519
4  4 -0.576429  1.987681 -0.322581
6  6 -0.036627 -0.380702 -0.170813
8  8 -1.000333  0.978760  0.256645

thanks!

Yi Zhang
  • 219
  • 1
  • 2
  • 5
  • Did you have a look at the following posts? http://stackoverflow.com/questions/14661701/how-to-drop-a-list-of-rows-from-pandas-dataframe http://stackoverflow.com/questions/37502298/python-pandas-get-index-from-column-value – Stijn Van Daele Dec 06 '16 at 15:38
  • Seems like column one is just the same as the index ... Is it ? – MMF Dec 06 '16 at 15:39

2 Answers2

4

You can use the .isin() method:

con = [2,4,6,8]
xxx[xxx["1"].isin(con)]

enter image description here

Psidom
  • 209,562
  • 33
  • 339
  • 356
3

Using isin

In [29]: df[df['1'].isin(con)]
Out[29]:
   1         2         3         4
2  2  0.707564 -0.270639 -0.251519
4  4 -0.576429  1.987681 -0.322581
6  6 -0.036627 -0.380702 -0.170813
8  8 -1.000333  0.978760  0.256645
Zero
  • 74,117
  • 18
  • 147
  • 154