2

I'm a newbie trying to go through Python for Data Analysis. I've been able to follow along and think Pandas is fantastic.

However, the example on page 127 uses the deprecated .ix method and I've tried to rework it a couple of times and am stumped.

Primarily I referenced the thorough explanation here: How are Pandas iloc, ix and loc different and related?

Specifically the section titled 'Simultaneous selection with labels and integer location'

Here's the example in the book:

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

[In]: data.ix[['Colorado', 'Utah'], [3, 0, 1]]
[Out]:
four one two
Colorado 7 0 5
Utah 11 8 9

And here's my example, based of the SO article, that I can't get to work:

labels = ['Colorado','Utah']
ind_names = data.index.get_loc()
index_ints = [df.index.get_loc(label) for label in labels]
print(data.iloc[index_ints,[3,0,1]])

Thank you for your help.

Programming_Learner_DK
  • 1,509
  • 4
  • 23
  • 49

2 Answers2

1

You need change df to data in list comprehension:

labels = ['Colorado','Utah']

index_ints = [data.index.get_loc(label) for label in labels]
print(data.iloc[index_ints,[3,0,1]])
          four  one  two
Colorado     7    4    5
Utah        11    8    9

Or use Index.get_indexer for positions by index names:

print(data.iloc[data.index.get_indexer(labels),[3,0,1]])
#alternatives
#print(data.iloc[data.index.searchsorted(labels),[3,0,1]])
          four  one  two
Colorado     7    4    5
Utah        11    8    9

Detail:

print(data.index.get_indexer(labels))
[1 2]
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • 1
    curious because you're always so helpful - why use list comprehension instead of the simple .loc[]? Generally curious as I do this operation quite a bit, but wondering if there are reasons to do it like you showed above? Thanks! – keg5038 Jan 10 '19 at 14:37
  • 2
    @keg5038 - answer is easy - first solution show how is necessary modify code of OP for working, but sure, better is second solution with no list comprehension. :) – jezrael Jan 10 '19 at 14:39
1

You can use label-based indexing via loc:

data.loc[['Colorado', 'Utah'], data.columns[[3, 0, 1]]]
jpp
  • 159,742
  • 34
  • 281
  • 339