Using pandas loc in hy
Asked Answered
B

1

6

I want to do the following in hy:

from StringIO import StringIO
import pandas as pd

s = """sepal_length  sepal_width  petal_length  petal_width species
0           5.1          3.5           1.4          0.2  setosa
1           4.9          3.0           1.4          0.2  setosa
2           4.7          3.2           1.3          0.2  setosa
3           4.6          3.1           1.5          0.2  setosa
4           5.0          3.6           1.4          0.2  setosa
5           5.4          3.9           1.7          0.4  setosa
6           4.6          3.4           1.4          0.3  setosa
7           5.0          3.4           1.5          0.2  setosa
8           4.4          2.9           1.4          0.2  setosa
9           4.9          3.1           1.5          0.1  setosa"""

df = pd.read_table(StringIO(s), sep="\s+")

df.loc[df.sepal_length > 4.5]

How do I do the last sentence?

I have tried (.loc df (> df.sepal_length 4.5))

but it just returns a locindexer.

Bass answered 11/11, 2015 at 17:32 Comment(2)
With this tagging it will probably be very difficult to attract viewers.Morion
How should I improve it :) ?Bass
H
5

There are two ways to do this:

  1. Using the . macro:

    (. df loc [(> df.sepal-length 4.5)])
    
  2. Using get:

    (get df.loc (> df.sepal-length 4.5))
    

Protip: always try running hy2py on your Hy files. It shows you what the resulting Python looks like. The output isn't always valid syntax, but it shows you what gets compiled into what. Both of these get compiled down to df.loc[(df.sepal_length > 4.5)].

One more thing: notice I used sepal-length. Hy converts dashes in identifiers to underscores, so that's the same thing as sepal_length, but it's considered better style.

Headlock answered 12/11, 2015 at 18:9 Comment(1)
@TheUnfunCat You're welcome! Next time you have a question, try the Hy mailing list.Headlock

© 2022 - 2024 — McMap. All rights reserved.