pandas - 在 hy 中使用 pandas loc

标签 pandas hy

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

最佳答案

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

  • 使用 . macro :
    (. df loc [(> df.sepal-length 4.5)])
    
  • 使用 get :
    (get df.loc (> df.sepal-length 4.5))
    

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

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

    关于pandas - 在 hy 中使用 pandas loc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33656372/

    相关文章:

    hy - hy 语言中有没有办法在 self 上使用 doto ?

    python - 在 Hy 中指定元类的语法

    python - Numpy 获取每列的 dtype

    python - 如何在 sklearn 中将线性回归与分类变量结合使用

    python - 如何将多键字典转换为 Pandas 数据框,其中每个键和值都有自己的列?

    python - 合并 pandas 数据帧 : keep row redundancy while removing column redundancy

    python - 查找数据框列中以字符串开头的所有元素

    海浪 : Howto simplest for loop in hy?

    python3/hy - 评估 python 中的 hy 表达式?

    python - 在 Hy 中使用 numpy.nditer