【问题标题】:Create dictionary based on matrix data frame index and a second data frame value基于矩阵数据框索引和第二个数据框值创建字典
【发布时间】:2020-10-08 01:20:46
【问题描述】:

我使用 pandas df= pd.read_csv("Example.csv", header=0, index_col="Forest") 导入了以下 CSV 数据框 df(数值为距离)

Forest,Bell Bay,Surrey Hills,Smithton,Hobart
Coupe 1,158,194,10,49
Coupe 2,156,169,71,84
Coupe 3,10,186,101,163
Coupe 4,47,94,134,139
Coupe 5,144,61,135,56
Coupe 6,27,27,134,36
Coupe 7,114,4,143,113
Coupe 8,71,170,190,140
Coupe 9,94,54,73,128
Coupe 10,46,194,92,36

我使用df2 = pd.read_csv("ExampleSupply.csv", header=0, index_col="Forest") 导入了第二个数据框df2(数值是供应数量)

Forest,Supply
Coupe 1,600
Coupe 2,100
Coupe 3,900
Coupe 4,300
Coupe 5,300
Coupe 6,400
Coupe 7,900
Coupe 8,700
Coupe 9,500
Coupe 10,300

Forest 列在两个数据框之间匹配。 我从df 创建了一个森林列表I 使用:

I = df.index.tolist()

结果:

['Coupe 1', 'Coupe 2', 'Coupe 3', 'Coupe 4', 'Coupe 5', 'Coupe 6', 'Coupe 7', 'Coupe 8', 'Coupe 9', 'Coupe 10']

以及J 中的目的地列表df 使用:

J = df.columns.values.tolist()

结果:

['Bell Bay', 'Surrey Hills', 'Smithton', 'Hobart']

元组(弧)列表是使用以下方法创建的:

arcs = [(i, j) for i in I for j in J]

结果:

[('Coupe 1', 'Bell Bay'), ('Coupe 1', 'Surrey Hills'), ('Coupe 1', 'Smithton'), ('Coupe 1', 'Hobart'), ('Coupe 2', 'Bell Bay'), ('Coupe 2', 'Surrey Hills'), ('Coupe 2', 'Smithton'), ('Coupe 2', 'Hobart'), ('Coupe 3', 'Bell Bay'), ('Coupe 3', 'Surrey Hills'), ('Coupe 3', 'Smithton'), ('Coupe 3', 'Hobart'), ('Coupe 4', 'Bell Bay'), ('Coupe 4', 'Surrey Hills'), ('Coupe 4', 'Smithton'), ('Coupe 4', 'Hobart'), ('Coupe 5', 'Bell Bay'), ('Coupe 5', 'Surrey Hills'), ('Coupe 5', 'Smithton'), ('Coupe 5', 'Hobart'), ('Coupe 6', 'Bell Bay'), ('Coupe 6', 'Surrey Hills'), ('Coupe 6', 'Smithton'), ('Coupe 6', 'Hobart'), ('Coupe 7', 'Bell Bay'), ('Coupe 7', 'Surrey Hills'), ('Coupe 7', 'Smithton'), ('Coupe 7', 'Hobart'), ('Coupe 8', 'Bell Bay'), ('Coupe 8', 'Surrey Hills'), ('Coupe 8', 'Smithton'), ('Coupe 8', 'Hobart'), ('Coupe 9', 'Bell Bay'), ('Coupe 9', 'Surrey Hills'), ('Coupe 9', 'Smithton'), ('Coupe 9', 'Hobart'), ('Coupe 10', 'Bell Bay'), ('Coupe 10', 'Surrey Hills'), ('Coupe 10', 'Smithton'), ('Coupe 10', 'Hobart')]

我想为df 中的row index(指I 中的Forest)和df2 中的数量值创建一个字典。字典应该是这样的:

dQ = {'Coupe 1': 600, 'Coupe 2': 100, 'Coupe 3': 900, 'Coupe 4': 300, 'Coupe 5': 300, 'Coupe 6': 400, 'Coupe 7': 900, 'Coupe 8': 700, 'Coupe 9': 500, 'Coupe 10': 300}

df2 中的数量值Supply 应与df 中的Forest 链接,请参考列表Iarcs 中的键。

谁能建议制定这本词典的最佳方法?这只是组合矩阵中的一小部分 I(10) 和 J(4)。我的方法必须适用于具有超过 1000 万个 I*J 组合的非常大的数据集。非常感谢您的帮助!

【问题讨论】:

    标签: python pandas numpy dataframe matrix


    【解决方案1】:

    您必须遍历列表 I 并在 df2 中找到相应的行,然后选择 df2['Supply'] 并将其添加到所需的字典中。

    dQ = {}
    for forest in I:
        dQ[forest] = df2.loc[forest]['Supply']
    print(dQ)
    

    结果:

    {'Coupe 1': 600, 'Coupe 2': 100, 'Coupe 3': 900, 'Coupe 4': 300, 'Coupe 5': 300, 'Coupe 6': 400, 'Coupe 7': 900, 'Coupe 8': 700, 'Coupe 9': 500, 'Coupe 10': 300}
    

    Pythonic 方式 -

    dQ = {forest: df2.loc[forest]['Supply'] for forest in I}
    

    问题 - 为什么您需要从 df 查找“森林”,因为您的 df2 已经具有相同的索引并且“森林”值匹配。如果您可以只使用 df2 数据并执行相同操作 - 将 df2 转换为 dict 使用

    df2.to_dict()['Supply']
    

    它也应该给你同样的结果

    【讨论】:

    • 感谢您的帮助。这行得通。不确定,对它很新。这两个数据框是独立的,我想通过检查Forest 名称来确保df2 中的信息与df 中的信息相关联。在我的一组约束中,我使用 supplydistance 值,并希望确保它们连接到相同的 forest
    猜你喜欢
    • 2020-10-06
    • 1970-01-01
    • 2021-10-13
    • 1970-01-01
    • 2018-11-06
    • 1970-01-01
    • 2013-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多