Anonamous Core
Anonamous Core

Reputation: 35

Indexing Selection problems

data = DataFrame(np.arange(16).reshape((4, 4)),
    index=['Ohio', 'Colorado', 'Utah', 'New York'],
    columns=['one', 'two', 'three', 'four'])

outputs:

>>> data
           one  two  three  four
Ohio        0    1      2     3
Colorado    4    5      6     7
Utah        8    9     10    11
New York   12   13     14    15

I now want to:

  1. filter this data frame and only output rows where ['three'] > 5
  2. output only the 1st 3 rows... so ignore the final one [:2]

I write this:

data.iloc[data['three']>5, [:2]]

and get this error:

    >>> data.iloc[data['three']>5, [:2]]
         File "<stdin>", line 1
         data.iloc[data['three']>5, [:2]]
                                     ^

how do I fix this??

these are my imports

import pandas as pd
from pandas import Series, DataFrame
import numpy as np

Upvotes: 2

Views: 70

Answers (1)

not_yet_a_fds
not_yet_a_fds

Reputation: 316

Your question is a bit unclear but what I have understood is you need Colorado and Utah in your final output. If that is the case, change the code to :

data[data['three']>5][:2]

Upvotes: 1

Related Questions