Python 在中,.loc未返回指定的行

Python 在中,.loc未返回指定的行,python,pandas,Python,Pandas,我试着让白天的工作时间小于100小时,但这是行不通的 temp_df = temp_df.loc[df['Hours'] < 100] 我应该补充一点,它过滤掉的行的“小时数”也远远超过100 下面的代码是有效的,但是.loc的错误在哪里呢 temp_df = temp_df.query('Hours <= 100')[['Date', 'Activity', 'Weekday', 'Hours']].reset_index(drop=True) 我应该在工作日列中得到不是“星期

我试着让白天的工作时间小于100小时,但这是行不通的

temp_df = temp_df.loc[df['Hours'] < 100]
我应该补充一点,它过滤掉的行的“小时数”也远远超过100

下面的代码是有效的,但是.loc的错误在哪里呢

temp_df = temp_df.query('Hours <= 100')[['Date', 'Activity', 'Weekday', 'Hours']].reset_index(drop=True)
我应该在工作日列中得到不是“星期六”或“星期日”的值,对吗

它回来了

+---+------------+----------+----------+-----------+
|   |    Date    | Activity | Weekday  |   Hours   |
+---+------------+----------+----------+-----------+
| 0 | 2020-06-06 | Gen Ab   | Saturday | 42.500000 |
| 1 | 2020-06-07 | Gen Ab   | Sunday   | 8.500000  |
| 2 | 2020-06-13 | Gen Ab   | Saturday | 24.183333 |
| 3 | 2020-06-20 | Gen Ab   | Saturday | 34.000000 |
| 4 | 2020-06-27 | Gen Ab   | Saturday | 25.500000 |
| 5 | 2020-07-03 | Gen Ab   | Friday   | 33.083333 |
| 6 | 2020-07-04 | Gen Ab   | Saturday | 18.500000 |
| 7 | 2020-07-11 | Gen Ab   | Saturday | 22.550000 |
+---+------------+----------+----------+-----------+

感谢所有像我这样的noob,这里的答案要感谢@Michael Butscher

If "temp_df" and "df" aren't the same initially, this may fail. Better: temp_df = df.loc[df ... 

我不知道他们不一样。我以为它们是,但回去检查了,它们不是。

重要的是检查用于写入选择条件的数据帧(或序列)是否与应用结果选择(布尔数组)的帧相同或至少相等

在这种情况下

temp_df.loc[df['Hours'] < 100]

如果“temp_df”和“df”最初不相同,则可能会失败。更好:
temp\u df=df.loc[df…
你就是炸弹!如果你把它作为一个答案,我会把它标记为正确,否则我会用你说的回答它
If "temp_df" and "df" aren't the same initially, this may fail. Better: temp_df = df.loc[df ... 
temp_df.loc[df['Hours'] < 100]
df.loc[df['Hours'] < 100]