Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using pandas loc in hy

Tags:

pandas

hy

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.

like image 470
The Unfun Cat Avatar asked Sep 26 '22 05:09

The Unfun Cat


1 Answers

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.

like image 172
kirbyfan64sos Avatar answered Sep 30 '22 07:09

kirbyfan64sos