比较两个数据帧以获得另一个数据帧

2024-05-14 08:47:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我在数据帧df上使用了df.describe( ),结果是:

Index  count               mean          std           min            25%           max 
ABC       6                5.14         4.63             1              2            12
CDE       6                18.6         12.5             2            7.5            15
DEF       7                30.2         32.6             1            6.5            20

另一个数据帧df2

 Index              Date        eventName    eventCount
 0            2017-08-09              ABC            24
 1            2017-08-09              CDE           140
 2            2017-08-10              CDE           150
 3            2017-08-11              DEF           200

其中阈值列应计算为:

例如,对于事件名称:ABC使用数据帧df2中的日期:2017-08-09(每日)作为新数据帧中的新索引,存储在新数据帧中的日期:2017-08-09的偏差百分比将是表示数据帧df2中ABC的eventCount列中的值:24减去该值在数据帧dfmin列中,也就是1代表ABC:24-1=23,然后除以df2*(100)中ABC的事件计数值,即23/24*100=95%

生成的(新)数据帧应如下所示:

     Index         eventName   eventCount        threshold       min
2017-08-09               ABC           24        Under 95%         1
2017-08-09               CDE          140      Under 98.5%         2
2017-08-10               CDE          150      Under 99.3%         2
2017-08-11               DEF          200      Under 99.5%         1 

我已经卡住了,我该怎么办?你知道吗


Tags: 数据dfindexdefcount事件minmean
1条回答
网友
1楼 · 发布于 2024-05-14 08:47:04

使用^{}(默认情况下left)添加新列,然后执行一些算术运算:

df = df2.set_index('Date').join(df[['min']], on='eventName', how='left')
df['threshold'] = df['eventCount'].sub(df['min']).div(df['eventCount']).mul(100).round(2)
#if need preix with %
df['threshold1'] =(df['eventCount'].sub(df['min'])
                                   .div(df['eventCount'])
                                   .mul(100)
                                   .apply("Under {0:.2f}%".format))
print (df)

           eventName  eventCount  min  threshold    threshold1
Date                                                          
2017-08-09       ABC          24    1      95.83  Under 95.83%
2017-08-09       CDE         140    2      98.57  Under 98.57%
2017-08-10       CDE         150    2      98.67  Under 98.67%
2017-08-11       DEF         200    1      99.50  Under 99.50%

相关问题 更多 >

    热门问题