简体   繁体   中英

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). 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.

How can I create the dataframe efficently, rather that iterate on the "mapping" entries?

Though I used for loop too, how about this?

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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