pandas-filter用作存储变量时仅对1行有效?

oogrdqng  于 2022-10-23  发布在  其他
关注(0)|答案(1)|浏览(111)

loc中使用过滤器时出现了一个奇怪的问题。
示例df:

Name trail
0    XYZ  True
1     A    True
2     B    True
3     C    True

# Trail filter

filter_trail = (df['trail'] == False)

# Set a row to False to check

df.at[3, 'trail'] = False

# use the filter, using loc because I will combine conditions

df.loc[filter_trail]

# I get the expected result

# Test further

df.at[0, 'trail'] = False

# use loc statement from earlier

# result only shows the 1st row i.e. the row with index 3

# No error in terminal

# decide to try dropping the column and setting column

df.drop('trail', axis=1, inplace=True)
df['trail'] = [True, False, False, False]

# run loc

df.loc[filter_trail]

# result still only shows row with index 3

# run without loc

df[filter_trail]

# result still only shows row with index 3

# run

df[df['trail'] == False]

# Get the desired result i.e. row index: 1,2,3

我不知道我在这里做错了什么。以前从未见过这种情况。

6ojccjat

6ojccjat1#

filter_trail不是作为对Dataframe的引用创建的,而是根据DF的trail列之一创建的布尔计算值。因此,创建一组新的数据,该数据是从DF列计算的,但不引用它。
两位stackoverflow贡献者(上面的评论)和我作为第三个尝试了您的代码,我们都收到了一个空的filter_trail。

相关问题