2016-02-27 70 views
0

我在Windows上運行Python 2.7中的下面的代碼。爲什麼我的浮點數變成Numpy Timeseries的一個值?

當我運行代碼(在所述柱的底部)收到錯誤:

Traceback (most recent call last): 

    File "<ipython-input-27-f7c0cd1d93c7>", line 75, in <module> 
    if r_val_path == 0: 

    File "C:\Anaconda\lib\site-packages\pandas\core\generic.py", line 698, in __nonzero__ 
    .format(self.__class__.__name__)) 

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). 

運行代碼後,我意識到r_valr_val_edger_val_path都是numpytimeseries'長度等於1或0而不是浮點數。我甚至在代碼的開頭清楚地分配了r_val = 0

有人可以告訴我爲什麼這是發生在我的代碼中嗎?

import pandas as pd 
import networkx as nx 
from itertools import permutations 
import numpy as np 

'''import dataframes ''' 
df = pd.DataFrame({'fld1': ['a', 'a', 'b', 'c', 'c', 'g', 'd', 'd', 'e', 'e', 'f'] 
       , 'fld2': ['b', 'c', 'f', 'd', 'g', 'd', 'e', 'b', 'c', 'f', 'b'] 
       , 'r_val': [0.1, 0.9, 1, 0.5, 0.5, 1, 0.8, 0.2, 0.2, 0.8, 1]}) 

##df of all relationships to build 
flds = pd.Series(df.fld1.unique()) 
flds = pd.Series(flds.append(pd.Series(df.fld2.unique())).unique()) 

combos = [] 
for L in range(0, len(flds)+1): 
    for subset in permutations(flds, L): 
     if len(subset) == 2: 
      combos.append(subset) 
     if len(subset) > 2: 
      break 

rel_df = pd.DataFrame.from_records(data = combos, columns = ['fld1','fld2']) 
rel_df['relationship'] = 0 

'''build graph ''' 

w_edges= map(list, df.values) 

DG=nx.DiGraph() 
DG.add_weighted_edges_from(w_edges) 

''' iterator ''' 
#iterate through each row of the rel_df 
for index, row in rel_df.iterrows(): 
    #pull source and target  
    fld1_val = rel_df.fld1[index] 
    fld2_val = rel_df.fld2[index] 
    #pull original r_val, vlookup both fields in df   
    try:  
     r_val = df.loc[(df['fld1'] == fld1_val) & 
         (df['fld2'] == fld2_val)]['r_val'] 
    except: 
     r_val = 0 
    #iterate through each path 
    for path in nx.all_simple_paths(DG, source= fld1_val, target= fld2_val):  
     path_holder = path 
     r_val_path = 0 
     r_val_path = r_val_path 
     #iterate through each edge in each path 
     for e in np.arange(0,len(path_holder)): 
      r_val_edge = 0    
      #grab nodes in pairs 
      if (e < len(path_holder)) & (
        (path_holder[e - 1 ] <> fld1_val) & 
        (path_holder[e] <> fld2_val)): 
       #grab pair of nodes 
       node1 = path_holder[e - 1] 
       node2 = path_holder[e] 
       #find r_val_edge from table 
       r_val_edge = df.loc[(df['fld1'] == node1) & 
         (df['fld2'] == node2)]['r_val'] 
       r_val_edge = r_val_edge  
       #add r_val_edge to r_val_path 
       if r_val_path == 0: 
        r_val_path = r_val_edge 
       else: 
        r_val_path = r_val_path * r_val_edge 
      else: 
       #path is done or path is direct connection 
       # move onto the next path 
       pass 

     r_val += r_val_path  
     #if the r_val for the path is less than threshold then quit     
     if r_val < .00000001: 
      pass 

    #if the r_val for the path is less than threshold then quit     
    if r_val < .00000001: 
     r_val = 0   
     pass 

    #add r_val to rel_df 
    rel_df.loc[(rel_df.fld1 == fld1_val) & (
       rel_df.fld2 == fld2_val),'relationship'] = r_val 

回答

1

pandas.loc輸出可以在一般返回多於一個的位置,請參閱this link with examples

+0

我通過在管線50和加入 「r_val =浮子(r_val)」 固定的這個問題「r_val_edge =浮子(r_val_edge) 「在第72行 – BeeGee

相關問題