假设我有一个DataFrame
这样的,
df = pd.DataFrame([['x', 1, 2], ['x', 1, 3], ['y', 2, 2]],
columns=['a', 'b', 'c'])
c == 2
要选择and的所有行a == 'x'
,我可以做类似的事情,
df[(df['a'] == 'x') & (df['c'] == 2)]
或者我可以通过制作临时变量来迭代细化,
df1 = df[df['a'] == 'x']
df2 = df1[df1['c'] == 2]
有没有办法对行进行迭代优化?
(
df
.refine(lambda row: row['a'] == 'x') # this method doesn't exist
.refine(lambda row: row['c'] == 2)
)