无法将函数输出分配给列

时间:2019-05-16 05:24:19

标签: python pandas

我有一个称为location的数据框,看起来像:

Home Number       Workplace Number      Year
A                 D                     1998
B                 H                     2001
C                 F                     2012
D                 O                     2005

我已经使用“家庭电话”创建了网络图。在这里,我要根据家庭网络计算家庭和工作场所之间的最小路径长度。对于给定的家庭和工作场所,我可以将它们插入以下功能:

def get_path_length(home_id, workplace_id):
    if home_id in nodes:
        try:
            path_length = nx.shortest_path(G, source = home_id, target = workplace_id)   
            print(path_length)
        except nx.NetworkXNoPath:
            path_length = -8888
    else:
        path_length = -9999
    return(path_length)

当我对数据框中的每一行应用相同的函数,然后将结果存储在称为“路径长度”的列中时,就会出现问题。我尝试了以下方法:

location_data = locations[['Home Number','Workplace Number']]

locations['Path Length'] = location_data.apply(get_path_length,axis=1)

但是我得到一个错误:

TypeError: ("get_path_length() missing 1 required positional argument: 'Workplace Number'", 'occurred at index 0')

我也尝试遍历每一行:

for index, row in locations.iterrows():
    locations[index,'Path Length'] = get_judge_path(row['Home Number'],row['Workplace Number'])

但随后出现错误:

ValueError: Length of values does not match length of index

有人知道如何解决此问题吗?

2 个答案:

答案 0 :(得分:1)

您可以在lambda函数中传递2列:

f = lambda x: get_path_length(x['Home Number'], x['Workplace Number'])
locations['Path Length'] = locations.apply(f, axis=1)

答案 1 :(得分:1)

您可以做到

 def get_path_length(arr):
    home_id, workplace_id, _ = arr
    if home_id in nodes:
        try:
            path_length = nx.shortest_path(G, source = home_id, target = workplace_id)   
            print(path_length)
        except nx.NetworkXNoPath:
            path_length = -8888
    else:
        path_length = -9999
    return(path_length)

locations['Path Length'] = locations.apply(get_path_length, axis=1)

第一次写答案,如果可以的话,希望能给我一个喜欢,非常感谢!