基于选择的日期的Python数据帧联接

假设我有以下变量和数据框架。

a = '2020-04-23 14:00:00+00:00','2020-04-23 13:00:00+00:00','2020-04-23 12:00:00+00:00','2020-04-23 11:00:00+00:00','2020-04-23 10:00:00+00:00','2020-04-23 09:00:00+00:00','2020-04-23 08:00:00+00:00','2020-04-23 07:00:00+00:00','2020-04-23 06:00:00+00:00','2020-04-23 04:00:00+00:00'
b = '2020-04-23 10:00:00+00:00','2020-04-23 09:00:00+00:00','2020-04-23 08:00:00+00:00','2020-04-23 07:00:00+00:00','2020-04-23 06:00:00+00:00','2020-04-23 05:00:00+00:00','2020-04-23 04:00:00+00:00','2020-04-23 03:00:00+00:00','2020-04-23 02:00:00+00:00','2020-04-23 01:00:00+00:00'

aa = 7105.50,6923.50,6692.50,6523.00,6302.5,6081.5,6262.0,6451.50,6369.50,6110.00
bb = 6386.00,6221.00,6505.00,6534.70,6705.00,6535.00,7156.50,7422.00,7608.50,8098.00

df1 = pd.DataFrame()
df1['timestamp'] = a
df1['price'] = aa

df2 = pd.DataFrame()
df2['timestamp'] = b
df2['price'] = bb


print(df1)
print(df2)

我想把下面的行连接起来:

  1. df1的最上面一行是’2020-04-23 08:00:00+00:00′

  2. ‘2020-04-23 07:00:00+00:00’改为df2的最后一行。

为了说明问题,下面是数据框的样子。

c = '2020-04-23 14:00:00+00:00','2020-04-23 13:00:00+00:00','2020-04-23 12:00:00+00:00','2020-04-23 11:00:00+00:00','2020-04-23 10:00:00+00:00','2020-04-23 09:00:00+00:00','2020-04-23 08:00:00+00:00','2020-04-23 07:00:00+00:00','2020-04-23 06:00:00+00:00','2020-04-23 05:00:00+00:00','2020-04-23 04:00:00+00:00','2020-04-23 03:00:00+00:00','2020-04-23 02:00:00+00:00','2020-04-23 01:00:00+00:00'

cc = 7105.50,6923.50,6692.50,6523.00,6302.5,6081.5,6262.0,6534.70,6705.00,6535.00,7156.50,7422.00,7608.50,8098.00


df = pd.DataFrame()
df['timestamp'] = c
df['price'] = cc
print(df)

有什么想法吗?

解决方案:

您可以将 timestamp 列到 pd.date_time 对象,然后使用布尔索引和 pd.concat 来选择和合并它们。

df1.timestamp = pd.to_datetime(df1.timestamp)
df2.timestamp = pd.to_datetime(df2.timestamp)

dfs = [df1.loc[df1.timestamp >= pd.to_datetime("2020-04-23 08:00:00+00:00"),:],
       df2.loc[df2.timestamp <= pd.to_datetime("2020-04-23 07:00:00+00:00"),:]
       ]

df_conc = pd.concat(dfs)

给TA打赏
共{{data.count}}人
人已打赏
解决方案

从localStorage中检索对象在控制台日志中可以工作,但在页面上却不能。

2022-5-13 6:00:19

解决方案

xmpphp XMPP, 从php脚本中发送消息。

2022-5-13 6:00:22

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索