如何在Pandas中提取特定行

时间:2018-05-24 12:34:04

标签: python pandas object startswith

我有一只大熊猫的df:

df['timestamp']
26600          58161.0
26628    58161.0416667
26656    58161.0833333
26684        58161.125
26712    58161.1666667
26740    58161.2083333
26768         58161.25
26796    58161.2916667
26824    58161.3333333
26852        58161.375
26880    58161.4166667
26908    58161.4583333
26936          58162.0
26964    58162.0416645
26992    58162.0833333

我想只提取以" .0"

结尾的两行

我尝试过这个命令:

df['timestamp'].str.startswith(".0", na=False)

但它返回:

AttributeError: 'Series' object has no attribute 'startswith'

有一种简单的方法可以完成这项工作吗?

由于

2 个答案:

答案 0 :(得分:5)

如果timestamp是数字,则可以使用模%

df.loc[df.timestamp % 1 == 0]

           timestamp
index           
26600    58161.0
26936    58162.0

如果它不是数字,你可以这样做,但首先转换:

df.loc[df.timestamp.astype(float) % 1 == 0]

答案 1 :(得分:3)

一种方法是将您的float系列与int版本进行比较:

df.loc[df[df['timestamp'] == df['timestamp'].astype(int)]
相关问题