在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
我不知道我在这里做错了什么。以前从未见过这种情况。
1条答案
按热度按时间6ojccjat1#
filter_trail
不是作为对Dataframe的引用创建的,而是根据DF的trail列之一创建的布尔计算值。因此,创建一组新的数据,该数据是从DF列计算的,但不引用它。两位stackoverflow贡献者(上面的评论)和我作为第三个尝试了您的代码,我们都收到了一个空的filter_trail。