按日期拆分 pandas 数据框
我想创建一个 pandas 数据表,获取下面的字典 a
并添加从初始日期算起的 days_split
天数并创建一个表。因此,对于下面的字典,由于第一个日期值为 2/4/2022 1:33:40 PM
我想再添加 10 天,这将使第一个表的范围为2/4/2022 1:33:40 PM 和 2/14/2022 1:33:40 PM
之间。由于字典中的最后一个日期值为 2/16/2022 9:43:30 AM
,因此它将在 2/14/ 的基础上添加另一个
因此它将创建第二个表范围为 days_split
天数2022 1:33:40 PM2/14/2022 1:33:40 PM 和 2/24/2022下午 1:33:40。所有字典值都可以容纳在这两个表中,因此无需创建更多表。我如何才能将此函数添加到下面的代码中并实现下面的预期输出?
import pandas as pd
from datetime import timedelta
a = {'Date': {0: '2/4/2022 1:33:40 PM', 1: '2/7/2022 3:09:46 PM', 2: '2/11/2022 9:35:44 AM', 3: '2/14/2022 2:55:33 PM',
4: '2/14/2022 2:57:06 PM', 5: '2/14/2022 2:58:12 PM', 6: '2/16/2022 9:30:44 AM', 7: '2/16/2022 9:32:56 AM', 8: '2/16/2022 9:32:59 AM',
9: '2/16/2022 9:43:30 AM'},
'TransactionType': {0: 'Buy ', 1: 'Buy ', 2: 'Buy ', 3: 'Sell ', 4: 'Sell ',
5: 'Sell ', 6: 'Buy ', 7: 'Buy ', 8: 'Buy ', 9: 'Buy '},
'Symbol': {0: 'META', 1: 'BABA', 2: 'GOOS', 3: 'BABA', 4: 'GOOS', 5: 'META', 6: 'DIS', 7: 'MAR',
8: 'ABNB', 9: 'EXPE'},
'QTY': {0: 185, 1: 21, 2: 75, 3: -21, 4: -75, 5: -185, 6: 10, 7: 9, 8: 10, 9: 9},
'Price': {0: 12.79, 1: 116.16, 2: 28.8, 3: 121.82, 4: 28.06, 5: 12.44, 6: 154.55,
7: 178.75, 8: 179.61, 9: 211.97},
'Amount': {0: -2366.15, 1: -2439.36, 2: -2160.0, 3: 2558.22, 4: 2104.5, 5: 2301.4, 6: -1545.5, 7: -1608.75,
8: -1796.1, 9: -1907.73}}
days_split = 10
df = pd.DataFrame(a)
预期产出:
表 1 2/4/2022 1:33:40 PM 至 2/14/2022 1:33:40 PM
表 2 2/14/2022 1:33:40 PM 至 2/24/2022 1 :33:40 PM
I would like to create a pandas datasheet that gets the dictionary a
below and adds days_split
amount of days from the initial date and creates a table. So for the dictionary below since the first date value is 2/4/2022 1:33:40 PM
I would like to add another 10 days into it which would make the range of the first table to be between 2/4/2022 1:33:40 PM and 2/14/2022 1:33:40 PM
. Since the last date value in the dictionary is 2/16/2022 9:43:30 AM
it will add another days_split
amount of days to 2/14/2022 1:33:40 PM
so it will create the second tables ranges from 2/14/2022 1:33:40 PM and 2/24/2022 1:33:40 PM
. All of the dictionary values could be housed within the 2 tables so there are no more tables that needs to be create. How would I be able to add this function to the code below and achieve the expected Output below?
import pandas as pd
from datetime import timedelta
a = {'Date': {0: '2/4/2022 1:33:40 PM', 1: '2/7/2022 3:09:46 PM', 2: '2/11/2022 9:35:44 AM', 3: '2/14/2022 2:55:33 PM',
4: '2/14/2022 2:57:06 PM', 5: '2/14/2022 2:58:12 PM', 6: '2/16/2022 9:30:44 AM', 7: '2/16/2022 9:32:56 AM', 8: '2/16/2022 9:32:59 AM',
9: '2/16/2022 9:43:30 AM'},
'TransactionType': {0: 'Buy ', 1: 'Buy ', 2: 'Buy ', 3: 'Sell ', 4: 'Sell ',
5: 'Sell ', 6: 'Buy ', 7: 'Buy ', 8: 'Buy ', 9: 'Buy '},
'Symbol': {0: 'META', 1: 'BABA', 2: 'GOOS', 3: 'BABA', 4: 'GOOS', 5: 'META', 6: 'DIS', 7: 'MAR',
8: 'ABNB', 9: 'EXPE'},
'QTY': {0: 185, 1: 21, 2: 75, 3: -21, 4: -75, 5: -185, 6: 10, 7: 9, 8: 10, 9: 9},
'Price': {0: 12.79, 1: 116.16, 2: 28.8, 3: 121.82, 4: 28.06, 5: 12.44, 6: 154.55,
7: 178.75, 8: 179.61, 9: 211.97},
'Amount': {0: -2366.15, 1: -2439.36, 2: -2160.0, 3: 2558.22, 4: 2104.5, 5: 2301.4, 6: -1545.5, 7: -1608.75,
8: -1796.1, 9: -1907.73}}
days_split = 10
df = pd.DataFrame(a)
Expected Output:
Table 1 between 2/4/2022 1:33:40 PM and 2/14/2022 1:33:40 PM
Table 2 between 2/14/2022 1:33:40 PM and 2/24/2022 1:33:40 PM
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用pandas.Grouper
Use
pandas.Grouper
我会使用 pd.Grouper,附加每个数据帧到一个列表:
输出:
I would use pd.Grouper, append each dataframe to a list:
Ouput: