简体   繁体   English

Pandas 数据框从交叉口的字典创建

[英]Pandas dataframe create from dict of crossings

I want to create a simple matrix where I have as index the name of a software requirement and as column all the software test cases in the project.我想创建一个简单的矩阵,其中我将软件需求的名称作为索引,并将项目中的所有软件测试用例作为列。

Where one SWRS is covered by one SWTS, I need to place "something" (for example a cross).如果一个 SWRS 被一个 SWTS 覆盖,我需要放置“某物”(例如一个十字架)。 In my code draft, I create an empty dataframe and then iterate to place the cross:在我的代码草稿中,我创建了一个空数据框,然后迭代以放置十字架:

import pandas as pd
struct = {
            "swrslist":["swrs1","swrs2","swrs3","swrs4"],
            "swtslist":["swts1","swts2","swts3","swts4","swts5","swts6"],
            "mapping":
                {
                    "swrs1": ["swts1", "swts3", "swts4"],
                    "swrs2": ["swts2", "swts3", "swts5"],
                    "swrs4": ["swts1", "swts3", "swts5"]
                }
           }

if __name__ == "__main__":
    df = pd.DataFrame( index = pd.Index(pd.Series(struct["swrslist"])),
                      columns = pd.Index(struct["swtslist"]))
    print(df)
    for key in struct["mapping"].keys():
        for elem in struct["mapping"][key]:
            print(key, elem)
            df.at[key,elem] = "x"
    print(df)
    df.to_excel("mapping.xlsx")

the output is the following输出如下

      swts1 swts2 swts3 swts4 swts5 swts6
swrs1     x   NaN     x     x   NaN   NaN
swrs2   NaN     x     x   NaN     x   NaN
swrs3   NaN   NaN   NaN   NaN   NaN   NaN
swrs4     x   NaN     x   NaN     x   NaN

I know that create an empty dataframe and then iterate is not efficient.我知道创建一个空数据框然后迭代效率不高。 I tried to create the dataframe as following我尝试按以下方式创建数据框

df = pd.DataFrame(struct["mapping"], index = pd.Index(pd.Series(struct["swrslist"])),
                      columns = pd.Index(struct["swtslist"]))

but it creates an empty dataframe:但它会创建一个空数据框:

      swts1 swts2 swts3 swts4 swts5 swts6
swrs1   NaN   NaN   NaN   NaN   NaN   NaN
swrs2   NaN   NaN   NaN   NaN   NaN   NaN
swrs3   NaN   NaN   NaN   NaN   NaN   NaN
swrs4   NaN   NaN   NaN   NaN   NaN   NaN

Furthermore, in future I plan to provide different values if a SWTS is a pass, fail or not executed.此外,如果 SWTS 是通过、失败或未执行,我计划在未来提供不同的值。

How can I create the dataframe efficently, rather that iterate on the "mapping" entries?如何有效地创建数据框,而不是迭代“映射”条目?

Though I used for loop too, how about this?虽然我也使用了for循环,但是这个怎么样?

df = pd.DataFrame(index=pd.Index(pd.Series(struct["swrslist"])), columns=pd.Index(struct["swtslist"]))
for key, value in struct["mapping"].items():
    df.loc[key, value] = "x"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM