Python 映射部分数据帧,并使用来自这两个数据帧的值添加新列值
我有两个不同长度的数据帧 数据帧A如下所示:Python 映射部分数据帧,并使用来自这两个数据帧的值添加新列值,python,pandas,datetime,dataframe,Python,Pandas,Datetime,Dataframe,我有两个不同长度的数据帧 数据帧A如下所示: time_A | Column_2 | Column_3 00:00:00 | Type A | ... ... | ... | ... 23:55:00 | Type A | ... 00:00:00 | Type B | ... ... | ... | ... 23:55:00 | Type B | ... 00:00:00 | Type C | ...
time_A | Column_2 | Column_3
00:00:00 | Type A | ...
... | ... | ...
23:55:00 | Type A | ...
00:00:00 | Type B | ...
... | ... | ...
23:55:00 | Type B | ...
00:00:00 | Type C | ...
... | ... | ...
23:55:00 | Type C | ...
time_B | Column_4 | Column_5
12/04/2019 00:00 | abc | ...
12/04/2019 00:00 | def | ...
... | ... | ...
12/04/2019 23:55 | ghi | ...
12/04/2019 23:55 | klm | ...
时间_A为字符串类型,包含的行数少于1000行
数据帧B如下所示:
time_A | Column_2 | Column_3
00:00:00 | Type A | ...
... | ... | ...
23:55:00 | Type A | ...
00:00:00 | Type B | ...
... | ... | ...
23:55:00 | Type B | ...
00:00:00 | Type C | ...
... | ... | ...
23:55:00 | Type C | ...
time_B | Column_4 | Column_5
12/04/2019 00:00 | abc | ...
12/04/2019 00:00 | def | ...
... | ... | ...
12/04/2019 23:55 | ghi | ...
12/04/2019 23:55 | klm | ...
时间_B也是字符串类型,它包含大约200000行
现在我有了一个时间范围作为datetime对象。比方说
[datetime.time(11,0),datetime.time(20,30)]
我想要的是为时间在上述范围内的所有行添加一个新列(列6)
单个列_6单元格的值如下所示:
对于时间在范围内的每个单元:
当前时间=当前时间
df_B.[“Column_6”]=每个_Column_3_cell_与_current_time/sum(Column_3_类型_x)*函数(每个_Column_3_cell)
或者举个例子:
if total_2025_typeC = 100 --> we added all type C column_3 for 20:25:00
and current_column_3 = 20 --> the only value for type C 20:25:00 is 20
then column_6 = (20/100) * formula(20)
到目前为止,我使用了:
newdf_index = df_B.index[(df_B["time_B"] >= range[0]) & (df_B["time_B"] <= range[1])].tolist()
只是想澄清一下:
[datetime.time(11,0),datetime.time(20,30)]
的意思是从11:00:00到20:30:00?@QuangHoang是的。我仍然不明白这两个值是如何结合在一起的total\u 2025\u typeC=100-->我们为20:25:00添加了所有类型C
和当前列\u 3=20-->类型C 20:25:00的唯一值。如果所有类型C 20:25加起来等于100,那么为什么20是唯一的值?这是我的错。我的意思是,100是范围[datetime.time(11,0),datetime.time(20,30)]
的总数,既然20:25:00
在该范围内,那么应该使用的总数是100。