python - 如何以函数式风格对数据框执行复杂的索引查询

标签 python pandas indexing

我喜欢使用query而不是loc,因为它可以与运算符链接一起使用,并且有时可以通过不重复DataFrame来保存字符> 姓名。

import pandas as pd
dataframe_with_a_long_name = pd.DataFrame({'animals': ['cat', 'dog']})
dataframe_with_a_long_name.query("animals == 'cat'")

如果我想做的事情不受查询(如str.contains)的支持,有没有办法做类似的事情?

dataframe_with_a_long_name[dataframe_with_a_long_name.animals.str.contains('c')]

最佳答案

0.18.1.loc[]、.iloc.ix 以及 []accept a callable这样你就可以:

dataframe_with_a_long_name.loc[lambda df: df.animals.str.contains('c')]

  animals
0     cat

关于python - 如何以函数式风格对数据框执行复杂的索引查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37201109/

相关文章:

python - 快速 Python 正则表达式问题 : Matching negated sets of characters

python - 使用索引以递归方式快速获取目录中的所有文件

mysql - 优化 MySQL 查询时出现问题

python - 为什么将我的模块分成多个文件会使它变慢?

python - 从先前图像集中绘制 1 个图像 + 从列表中随机绘制 2 个图像 (Python/Psychopy)

python - 打印指定范围内的列表Python

python - 使用 pandas Between_time() 函数并以列表作为输入参数

Python、Pandas 与 groupby 错误

Python pandas 停留在 0.7.0 版本

arrays - F# 两个数组 - 第一个数组乘积按第二个数组上的索引过滤