简体   繁体   中英

How to join 2 lists of lists together with multiple columns in python

I have these two lists of lists, the last column is the probability of this happening

#Pr(Fraud | Trav)
f3 = [[
    [True, True, 0.01],
    [False, True, 0.004],
    [True, False, 0.99],
    [False, False, 0.996]
], ["Trav", "Fraud"]]

f4 = [[
    [False, True, True, 0.1],
    [False, False, True, 0.01],
    [True, True, True, 0.9],
    [True, False, True, 0.9],
    [False, True, False, 0.9],
    [False, False, False, 0.99],
    [True, True, False, 0.1],
    [True, False, False, 0.1],
], ["Trav", "Fraud", "FP"]]

and I would like to join them together by Trav and Fraud to produce the following table:

[[True,True,True,0.009]
[True,True,False,0.001]
[True,False,True,0.891]
[True,False,False,0.099]
[False,True,True,0.0004]
[False,True,False,0.0036]
[False,False,True,0.00996]
[False,False,False,0.98604]],
["Trav","Fraud","FP"]]

Thanks in advance!!!

You can use panda.DataFrame.merge to merge them.

If you just want to merge two lists:

def merge_probability(f3, f4):
    df0 = pd.DataFrame(f3[0], columns=f3[1] + ["probability0"])
    df1 = pd.DataFrame(f4[0], columns=f4[1] + ["probability1"])

    df = df0.merge(df1)

    df["probability"] = df["probability0"] * df["probability1"]
    df = df.drop(["probability0", "probability1"], axis=1)
    return [df.values.tolist(), f4[1]]

Or if more lists need to be merged together:

def merge_probabilitys(*fs):
    size = len(fs)
    dfs = [
        pd.DataFrame(fs[i][0], columns=fs[i][1] + ["probability" + str(i)])
        for i in range(size)
    ]

    df_res = dfs[0]
    for df in dfs[1:]:
        df_res = df_res.merge(df)

    df_res["probability"] = 1
    for i in range(size):
        df_res["probability"] *= df_res["probability" + str(i)]

    df_res = df_res.drop(["probability" + str(i) for i in range(size)], axis=1)
    return [df_res.values.tolist(), list(df_res.columns)[:-1]]

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