【发布时间】:2020-10-03 04:16:22
【问题描述】:
这只是一个带有是或否答案的快速问题。我在谷歌或这里找不到答案(谷歌很难)。
我只是想知道我这样做是否正确。
我正在尝试选择符合某些条件的数据。这是我的代码片段。
c1 = (data['recency']<=3) # seen in the last 3 months
c2 = (data['transactions_per_month']>=1) # buys a ticket once a month
c3 = (data['av_spend_per_month']>=30) # spends at least €30 per month
c4 = (data['Driver']==1) # is a driver
# slice the df
data[c1 & (c2 | c3) & c4]
这部分正确吗? (c2 | c3) 我可以在我的& 条件中间添加一个| 条件吗?
如果错了,正确的做法是什么?
【问题讨论】:
-
是的,但它需要像
data[ (col1>10)or (col2<10)]一样,即通过过滤器而不是过滤数据。在此处查看 mroe pandas.pydata.org/pandas-docs/stable/user_guide/… -
@venky__ 所以你使用了
or而不是|。我是否需要将|更改为or? -
使用我应该写的
operators( & | )| -
应该更安全:
data[((c1) & ((c2) | (c3))) & (c4)]。条件定义周围的括号使它看起来像带有系列的元组对象,这就是我再次放置括号的原因