英文:
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
我编造了时间戳。假设我想要所有数据行在1640000000000000000
到1680000000000000000
之间,
即,我试图让最终结果看起来像:
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['timestamp'] > 1640000000000000000) & (df['timestamp'] < 1680000000000000000)]
If you want to include the numbers themselves, you can use >= and <= respectively.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论