How can I filter a dataframe for rows between two given timestamps?

huangapple go评论65阅读模式
英文:

How can I filter a dataframe for rows between two given timestamps?

问题

我有一个包含许多行和列的大型数据框,我想筛选出所有时间戳在特定范围内的行 - 我该如何做到这一点?

我的数据框:

        timestamp          data     size     price 
   1690000000000000000      1       100       55
   170000000000000000      5       20        100
   1680000000000000000      5       55         44
   1620000000000000000      4        4         33
   1650000000000000000      10       4         1 
   1640000000000000000      15       2         99

我编造了时间戳。假设我想要所有数据行在16400000000000000001680000000000000000之间,

即,我试图让最终结果看起来像:

        timestamp          data     size     price 
   1680000000000000000      5       55         44
   1650000000000000000      10       4         1 
   1640000000000000000      15       2         99
英文:

So I have a large dataframe with many rows and columns, and I am trying to filter it for all the rows that have timestamps within a certain range - how can i do this?

My dataframe:

        timestamp          data     size     price 
   1690000000000000000      1       100       55
   170000000000000000      5       20        100
   1680000000000000000      5       55         44
   1620000000000000000      4        4         33
   1650000000000000000      10       4         1 
   1640000000000000000      15       2         99

I have made up the timestamps. And let's say I want all the rows of data between
1640000000000000000 to 1680000000000000000

i.e. I am trying to get the end result to look like:

        timestamp          data     size     price 
   1680000000000000000      5       55         44
   1650000000000000000      10       4         1 
   1640000000000000000      15       2         99

答案1

得分: 2

df = df.loc[(df['timestamp'] > 1640000000000000000) & (df['timestamp'] < 1680000000000000000)]

如果要包括数字本身,可以分别使用 >= 和 <=。

英文:
df = df.loc[(df[&#39;timestamp&#39;] &gt; 1640000000000000000) &amp; (df[&#39;timestamp&#39;] &lt; 1680000000000000000)]

If you want to include the numbers themselves, you can use >= and <= respectively.

huangapple
  • 本文由 发表于 2023年7月12日 21:56:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76671372.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定