English 中文(简体)
将数据汇总到更高的组织级别
原标题:Rolling up data to higher organization level

我对Python相当陌生,我希望有人能在组织类型的数据方面帮助我。我有一个公司组织表(org_table),看起来像这样,注意组织级别中的某个地方可能有null,但最高和最低级别永远不会为null

Super Division Division Super Department Department Sub Department
123 342 767 546 965
345 453 234 na 759

我还有一个表,其中包含与组织相关联的“报告”(report_table),可以是任何级别的。所以它看起来像这样

Report Name Org Level Org ID
ABC Division 342
DEF Super Department 234
GHI Super Division 123

我想得到一个结果集,其中包含相同的报告列表,但现在已汇总到最高级别。这样的事情。我该如何做到这一点?

Report Name Org Level Org ID
ABC Division 342
ABC Super Division 123
DEF Super Department 234
DEF Division 453
DEF Super Division 345
GHI Super Division 123

我曾尝试编写一个检索汇总的函数,但结果集也为我提供了所有较低的级别。

`    def get_org_rollup(row):
        org_level = row[ Org Level ]
        org_rollups = []
        for i, level in enumerate(org_table.columns):
            if level == org_level:
                org_rollups.append(row[ Org ID ])
            elif not pd.isnull(org_table.iloc[row.name, i]):
                org_rollups.append(org_table.iloc[row.name, i])
            else:
                break
        return pd.Series(org_rollups, index=org_table.columns[:len(org_rollups)])`
问题回答

这里有一条非常简洁的单行线,供您在不创建函数的情况下尝试。

(pd.concat([df.where((df == x).iloc[:,::-1].cumsum(1).astype(bool)).melt().dropna() 
           for x in dfr[ Org ID ]], keys=dfr[ Report Name ])
   .reset_index()
   .drop( level_1 , axis=1))

输出:

  Report Name          variable  value
0         ABC    Super Division  123.0
1         ABC          Division  342.0
2         DEF    Super Division  345.0
3         DEF          Division  453.0
4         DEF  Super Department  234.0
5         GHI    Super Division  123.0

详细信息:

使用反转元素::-1,我将在每行上保留True的所有值,并使用cumsum。然后,使用这个布尔矩阵只返回那些使用的True值,其中。接下来,我们使用melt从宽转换为长,然后pd.使用列表理解按行连接以上结果,最后reset_index删除level_1列。





相关问题
what is wrong with this mysql code

$db_user="root"; $db_host="localhost"; $db_password="root"; $db_name = "fayer"; $conn = mysqli_connect($db_host,$db_user,$db_password,$db_name) or die ("couldn t connect to server"); // perform query ...

Users asking for denormalized database

I am in the early stages of developing a database-driven system and the largest part of the system revolves around an inheritance type of relationship. There is a parent entity with about 10 columns ...

Easiest way to deal with sample data in Java web apps?

I m writing a Java web app in my free time to learn more about development. I m using the Stripes framework and eventually intend to use hibernate and MySQL For the moment, whilst creating the pages ...

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

How can I know if such value exists in database? (ADO.NET)

For example, I have a table, and there is a column named Tags . I want to know if value programming exists in this column. How can I do this in ADO.NET? I did this: OleDbCommand cmd = new ...

Convert date to string upon saving a doctrine record

I m trying to migrate one of my PHP projects to Doctrine. I ve never used it before so there are a few things I don t understand. In my current code, I have a class similar to this: class ...

热门标签