我有一个3列的数据框,看起来像
Addresses Floor Time
0 fd00::212:4b00:1957:ce5f B2WL3 08:00:00
1 fd00::212:4b00:1957:ce4f B2WL2 08:00:00
2 fd00::212:4b00:1957:d184 B2WL4 08:00:00
3 fd00::212:4b00:1957:d1ac B2WL1 08:00:00
4 fd00::212:4b00:1957:ce21 B2EL4 08:00:00
5 fd00::212:4b00:1957:d186 B2WKK 08:00:00
6 fd00::212:4b00:1957:ce65 B2EL2 08:00:00
7 fd00::212:4b00:1957:e7d7 D3L2 07:30:00
8 fd00::212:4b00:1957:f02d D3L0 07:30:00
9 fd00::212:4b00:1957:f01f D2L1 07:30:00
我想过滤时间为8的行。当我做df.dtypes
时,所有列都是对象类型。我尝试过
df[df['Time']=='08:00:00']
但是这将返回一个空的数据帧。我不确定为什么会这样吗?有人可以帮我吗?
答案 0 :(得分:0)
时间列中有空或nan
值。
使用df[df['Time']=='08:00:00']
进行过滤应该可以。
我将示例数据集中的值之一替换为一个空字符串,现在dtypes
的值是对象,并且过滤给出了一个空的数据框
>>> df
Addresses Floor Time
0 fd00::212:4b00:1957:ce4f B2WL2 2019-12-13 08:00:00
1 fd00::212:4b00:1957:d184 B2WL4 2019-12-13 08:00:00
2 fd00::212:4b00:1957:d1ac B2WL1 2019-12-13 08:00:00
3 fd00::212:4b00:1957:ce21 B2EL4 2019-12-13 08:00:00
4 fd00::212:4b00:1957:d186 B2WKK
5 fd00::212:4b00:1957:ce65 B2EL2 2019-12-13 08:00:00
6 fd00::212:4b00:1957:e7d7 D3L2 2019-12-13 07:30:00
7 fd00::212:4b00:1957:f02d D3L0 2019-12-13 07:30:00
8 fd00::212:4b00:1957:f01f D2L1 2019-12-13 07:30:00
>>> df.dtypes
Addresses object
Floor object
Time object
dtype: object
>>> df[df.Time == '08:00:00']
Empty DataFrame
Columns: [Addresses, Floor, Time]
Index: []
所以我建议删除空单元格,然后重试
>>> df.Time = pd.to_datetime(df.Time.replace('', np.nan))
>>> df.dtypes
Addresses object
Floor object
Time datetime64[ns]
dtype: object
>>> df[df.Time == '08:00:00']
Addresses Floor Time
0 fd00::212:4b00:1957:ce4f B2WL2 2019-12-13 08:00:00
1 fd00::212:4b00:1957:d184 B2WL4 2019-12-13 08:00:00
2 fd00::212:4b00:1957:d1ac B2WL1 2019-12-13 08:00:00
3 fd00::212:4b00:1957:ce21 B2EL4 2019-12-13 08:00:00
5 fd00::212:4b00:1957:ce65 B2EL2 2019-12-13 08:00:00