pandas dataframe.query方法语法 [英] Pandas dataframe.query method syntax
问题描述
问题:
I would like to gain a better understanding of the Pandas DataFrame.query method and what the following expression represents:
match = dfDays.query('index > @x.name & price >= @x.target')
@x.name
代表什么?
我知道此代码(带有pandas.tslib.Timestamp
数据的新列)的输出结果是什么,但对用于获得此最终结果的表达式没有清楚的了解.
I understand what the resulting output is for this code (a new column with pandas.tslib.Timestamp
data) but don't have a clear understanding of the expression used to get this end result.
数据:
从这里:
np.random.seed(seed=1)
rng = pd.date_range('1/1/2000', '2000-07-31',freq='D')
weeks = np.random.uniform(low=1.03, high=3, size=(len(rng),))
ts2 = pd.Series(weeks
,index=rng)
dfDays = pd.DataFrame({'price':ts2})
dfWeeks = dfDays.resample('1W-Mon').first()
dfWeeks['target'] = (dfWeeks['price'] + .5).round(2)
def find_match(x):
match = dfDays.query('index > @x.name & price >= @x.target')
if not match.empty:
return match.index[0]
dfWeeks.assign(target_hit=dfWeeks.apply(find_match, 1))
推荐答案
@MaxU所说的一切都很完美!
Everything @MaxU said is perfect!
我想为应用此问题的特定问题添加一些上下文.
I wanted to add some context to the specific problem that this was applied to.
这是在数据框dfWeeks.apply
中使用的辅助函数.需要注意的两件事:
This is a helper function that is used in the dataframe dfWeeks.apply
. Two things to note:
-
find_match
采用单个参数x
.这将是dfWeeks
的一行.- 每一行都是一个
pd.Series
对象,每一行都将通过此函数传递.这是使用apply
的本质. - 当
apply
将此行传递给helper函数时,该行具有name
属性,该属性等于数据框中该行的索引值.在这种情况下,我知道索引值是pd.Timestamp
,我将使用它来进行所需的比较.
- 每一行都是一个
find_match
takes a single argumentx
. This will be a single row ofdfWeeks
.- Each row is a
pd.Series
object and each row will be passed through this function. This is the nature of usingapply
. - When
apply
passes this row to the helper function, the row has aname
attribute that is equal to the index value for that row in the dataframe. In this case, I know that the index value is apd.Timestamp
and I'll use it to do the comparing I need to do.
- Each row is a
我不必使用query
...我喜欢使用query
.我认为这会使一些代码更漂亮. OP提供的以下功能可以用不同的方式写
I didn't have to use query
... I like using query
. It is my opinion that it makes some code prettier. The following function, as provided by the OP, could've been written differently
def find_match(x):
"""Original"""
match = dfDays.query('index > @x.name & price >= @x.target')
if not match.empty:
return match.index[0]
dfWeeks.assign(target_hit=dfWeeks.apply(find_match, 1))
find_match_alt
或者我们可以这样做,这可能有助于解释query
字符串在上面的作用
find_match_alt
Or we could've done this, which may help to explain what the query
string is doing above
def find_match_alt(x):
"""Alternative to OP's"""
date_is_afterwards = dfDays.index > x.name
price_target_is_met = dfDays.price >= x.target
both_are_true = price_target_is_met & date_is_afterwards
if (both_are_true).any():
return dfDays[both_are_true].index[0]
dfWeeks.assign(target_hit=dfWeeks.apply(find_match_alt, 1))
比较这两个功能应该可以很好地理解
Comparing these two functions should give good perspective.
这篇关于 pandas dataframe.query方法语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!