如何在Pandas中提取特定行

And*_*rea 3 python object startswith pandas

我有一只熊猫的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
Run Code Online (Sandbox Code Playgroud)

我想只提取以".0"结尾的两行

我试过这个命令:

df['timestamp'].str.startswith(".0", na=False)
Run Code Online (Sandbox Code Playgroud)

但它返回:

AttributeError: 'Series' object has no attribute 'startswith'
Run Code Online (Sandbox Code Playgroud)

有一个简单的方法来完成这项工作吗?

谢谢

sac*_*cuL 5

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

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

           timestamp
index           
26600    58161.0
26936    58162.0
Run Code Online (Sandbox Code Playgroud)

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

df.loc[df.timestamp.astype(float) % 1 == 0]
Run Code Online (Sandbox Code Playgroud)