【问题标题】:Using pandas loc in hy在 hy 中使用 pandas loc
【发布时间】:2015-11-11 17:32:49
【问题描述】:

我想在 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]

最后一句怎么写?

我试过(.loc df (> df.sepal_length 4.5))

但它只返回一个 locindexer。

【问题讨论】:

  • 有了这个标签可能很难吸引观众。
  • 我应该如何改进它:)?

标签: pandas hy


【解决方案1】:

有两种方法可以做到这一点:

  1. 使用. macro

    (. df loc [(> df.sepal-length 4.5)])
    
  2. 使用get

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

Protip:始终尝试在您的 Hy 文件上运行 hy2py。它向您展示了生成的 Python 的样子。输出并不总是有效的语法,但它向您展示了编译成什么。这两个都被编译成df.loc[(df.sepal_length > 4.5)]

还有一件事:注意我使用了sepal-length。 Hy 将标识符中的破折号转换为下划线,因此这与 sepal_length 相同,但它被认为是更好的样式。

【讨论】:

  • @TheUnfunCat 不客气!下次有问题时,请尝试Hy mailing list
猜你喜欢
  • 2014-08-17
  • 2021-05-20
  • 1970-01-01
  • 2016-10-21
  • 2017-03-05
  • 2020-07-28
  • 1970-01-01
  • 2022-10-24
  • 2017-07-15
相关资源
最近更新 更多