为什么 pandas df.loc + lambda不起作用? [英] Why does not work pandas df.loc + lambda?
问题描述
我已经从csv文件创建了熊猫框架. 我想选择使用lambda的行. 但这是行不通的. 我使用了此熊猫手册. 例外:
I have created pandas frame from csv file. And I want to select rows use lambda. But it does not work. I use this pandas manual. exception:
出什么问题了? 谢谢.
what is problem? thanks.
推荐答案
正如@BrenBam在评论中说的那样,此语法已在0.18.1中添加,并且在以前的版本中不起作用.
As @BrenBam has said in the comment this syntax was added in 0.18.1 and it won't work in previous versions.
可调用选择:
.loc,.iloc,.ix和[]索引也可以接受可调用的 索引器.可调用对象必须是带有一个参数的函数( 调用Series,DataFrame或Panel),并为 索引.
.loc, .iloc, .ix and also [] indexing can accept a callable as indexer. The callable must be a function with one argument (the calling Series, DataFrame or Panel) and that returns valid output for indexing.
示例(0.18.1版):
Example (version 0.18.1):
In [10]: df
Out[10]:
a b c
0 1 4 2
1 2 2 4
2 3 4 0
3 0 2 3
4 3 0 4
In [11]: df.loc[lambda df: df.a == 3]
Out[11]:
a b c
2 3 4 0
4 3 0 4
对于< = 0.18.0版本,您不能使用Selection by callable
:
For versions <= 0.18.0 you can't use Selection by callable
:
请改用这种方式:
df.loc[df['Date'] == '2003-01-01 00:00:00', ['Date']]
这篇关于为什么 pandas df.loc + lambda不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!