根据所有组合的索引和分组创建内部python循环

时间:2017-04-06 16:31:31

标签: python dataframe enumerate

我有一个脚本,它查看属于组(REG_ID)的行和列标题并对值进行求和。代码在矩阵(小子集)上运行,如下所示:

Outputs

我的代码适用于根据属于每个内部组(REG_ID)的行和列计算所有ID的总和。例如,对于属于REG_ID 1的所有行和列ID进行求和,以便计算区域1和区域1(内部流)之间的总流量,等等,对于每个区域。 我希望通过计算(求和)区域之间的流量(例如区域1到区域2,3,4,5 ......)来扩展此代码。 我认为我需要在现有的while循环中包含另一个循环,但我真的很感激一些帮助,以确定它应该在哪里以及如何构造它。 我目前在内部流量总和(1-1,2-2,3-3等)上运行的代码如下:

global index
index = 1
x = index
while index < len(idgroups):
    ward_list = idgroups[index] #select list of ward ids for each region from list of lists
    df6 = mergedcsv.loc[ward_list] #select rows with values in the list
    dfcols = mergedcsv.loc[ward_list, :] #select columns with values in list
    ward_liststr = map(str, ward_list) #convert ward_list to strings so that they can be used to select columns, won't work as integers.
    ward_listint = map(int, ward_list)
    #dfrowscols = mergedcsv.loc[ward_list, ward_listint]
    df7 = df6.loc[:, ward_liststr]
    print df7
    regflowsum = df7.values.sum() #sum all values in dataframe
    intflow = [regflowsum]
    print intflow
    dfintflow = pd.DataFrame(intflow)
    dfintflow.reset_index(level=0, inplace=True)
    dfintflow.columns = ["RegID", "regflowsum"]
    dfflows.set_value(index, 'RegID', index)
    dfflows.set_value(index, 'RegID2', index)
    dfflows.set_value(index, 'regflow', regflowsum)
    mergedcsv.set_value(ward_list, 'TotRegFlows', regflowsum)
    index += 1 #increment index number
print dfflows
new_df = pd.merge(pairlist, dfflows,  how='left', left_on=['origID','destID'], right_on = ['RegID', 'RegID2'])
print new_df #useful for checking dataframe merges
regionflows = r"C:\Temp\AllNI\regionflows.csv"
header = ["WardID","LABEL","REG_ID","Total","TotRegFlows"]
mergedcsv.to_csv(regionflows, columns = header, index=False)
regregflows = r"C:\Temp\AllNI\reg_regflows.csv"
headerreg = ["REG_ID_ORIG", "REG_ID_DEST", "FLOW"]

pairlistCSV = r"C:\Temp\AllNI\pairlist_regions.csv"
new_df.to_csv(pairlistCSV)

输出如下:

idgroups dataframe :(见图片1 - 图片1的第二部分)

每个区域的df7和intflow Reg_ID :(图像1的第三部分 - 右侧)

ddflows dataframe :(图像2的第四部分)

,最终输出为new_df :(图像2的第五部分)

我希望在不仅仅是内部区域之间填充所有可能的流量组合的总和。

我想我需要在while循环中添加另一个循环。所以可能添加一个枚举函数,如:

while index < len(idgroups):
    #add line(s) to calculate flows between regions
    for index, item in enumerate(idgroups):
        ward_list = idgroups[index]
        print ward_list
        df6 = mergedcsv.loc[ward_list] #select rows with values in the list
        dfcols = mergedcsv.loc[ward_list, :] #select columns with values in list
        ward_liststr = map(str, ward_list) #convert ward_list to strings so that they can be used to select columns, won't work as integers.
        ward_listint = map(int, ward_list)
        #dfrowscols = mergedcsv.loc[ward_list, ward_listint]
        df7 = df6.loc[:, ward_liststr]
        print df7
        regflowsum = df7.values.sum() #sum all values in dataframe
        intflow = [regflowsum]
        print intflow
        dfintflow = pd.DataFrame(intflow)
        dfintflow.reset_index(level=0, inplace=True)
        dfintflow.columns = ["RegID", "regflowsum"]
        dfflows.set_value(index, 'RegID', index)
        dfflows.set_value(index, 'RegID2', index)
        dfflows.set_value(index, 'regflow', regflowsum)
        mergedcsv.set_value(ward_list, 'TotRegFlows', regflowsum)
        index += 1 #increment index number

我不确定如何整合项目,因此努力扩展所有组合的代码。任何建议表示赞赏。

基于流程功能更新:

    w=pysal.rook_from_shapefile("C:/Temp/AllNI/NIW01_sort.shp",idVariable='LABEL')
Simil = pysal.open("C:/Temp/AllNI/simNI.csv")
Similarity = np.array(Simil)
db = pysal.open('C:\Temp\SQLite\MatrixCSV2.csv', 'r')
dbf = pysal.open(r'C:\Temp\AllNI\NIW01_sortC.dbf', 'r')
ids = np.array((dbf.by_col['LABEL']))
commuters = np.array((dbf.by_col['Total'],dbf.by_col['IDNO']))
commutersint = commuters.astype(int)
comm = commutersint[0]
floor = int(MIN_COM_CT + 100)
solution = pysal.region.Maxp(w=w,z=Similarity,floor=floor,floor_variable=comm)
regions = solution.regions
#print regions
writecsv = r"C:\Temp\AllNI\reg_output.csv"
csv = open(writecsv,'w')
csv.write('"LABEL","REG_ID"\n')
for i in range(len(regions)):
        for lines in regions[i]:
            csv.write('"' + lines + '","' + str(i+1) + '"\n')
csv.close()
flows = r"C:\Temp\SQLite\MatrixCSV2.csv"
regs = r"C:\Temp\AllNI\reg_output.csv"
wardflows = pd.read_csv(flows)
regoutput = pd.read_csv(regs)
merged = pd.merge(wardflows, regoutput)
#duplicate REG_ID column as the index to be used later
merged['REG_ID2'] = merged['REG_ID']
merged.to_csv("C:\Temp\AllNI\merged.csv", index=False)
mergedcsv = pd.read_csv("C:\Temp\AllNI\merged.csv",index_col='WardID_1') #index this dataframe using the WardID_1 column
flabelList = pd.read_csv("C:\Temp\AllNI\merged.csv", usecols = ["WardID", "REG_ID"]) #create list of all FLabel values

reg_id = "REG_ID"
ward_flows = "RegIntFlows"
flds = [reg_id, ward_flows] #create list of fields to be use in search

dict_ref = {} # create a dictionary with for each REG_ID a list of corresponding FLABEL fields


#group the dataframe by the REG_ID column
idgroups = flabelList.groupby('REG_ID')['WardID'].apply(lambda x: x.tolist())
print idgroups

idgrp_df = pd.DataFrame(idgroups)

csvcols = mergedcsv.columns

#create a list of column names to pass as an index to select columns
columnlist = list(mergedcsv.columns.values)

mergedcsvgroup = mergedcsv.groupby('REG_ID').sum()
mergedcsvgroup.describe()
idList = idgroups[2]
df4 = pd.DataFrame()
df5 = pd.DataFrame()
col_ids = idList #ward id no

regiddf = idgroups.index.get_values()
print regiddf
#total number of region ids
#print regiddf
#create pairlist combinations from region ids
#combinations with replacement allows for repeated items
#pairs = list(itertools.combinations_with_replacement(regiddf, 2))
pairs = list(itertools.product(regiddf, repeat=2))
#print len(pairs)

#create a new dataframe with pairlists and summed data
pairlist = pd.DataFrame(pairs,columns=['origID','destID'])
print pairlist.tail()
header_pairlist = ["origID","destID","flow"]
header_intflow = ["RegID", "RegID2", "regflow"]
dfflows = pd.DataFrame(columns=header_intflow)

print mergedcsv.index
print mergedcsv.dtypes
#mergedcsv = mergedcsv.select_dtypes(include=['int64'])
#print mergedcsv.columns
#mergedcsv.rename(columns = lambda x: int(x), inplace=True)

def flows():
    pass

#def flows(mergedcsv, region_a, region_b):
def flows(mergedcsv, ward_lista, ward_listb):
    """Return the sum of all the cells in the row/column intersections
    of ward_lista and ward_listb."""

    mergedcsv = mergedcsv.loc[:, mergedcsv.dtypes == 'int64']
    regionflows = mergedcsv.loc[ward_lista, ward_listb]
    regionflowsum = regionflows.values.sum()


    #grid = [ax, bx, regflowsuma, regflowsumb]
    gridoutput = [ax, bx, regionflowsum]
    print gridoutput

    return regflowsuma
    return regflowsumb

#print mergedcsv.index

#mergedcsv.columns = mergedcsv.columns.str.strip()

for ax, group_a in enumerate(idgroups):
    ward_lista = map(int, group_a)
    print ward_lista


    for bx, group_b in enumerate(idgroups[ax:], start=ax):
        ward_listb = map(int, group_b)
        #print ward_listb

        flow_ab = flows(mergedcsv, ward_lista, ward_listb)
            #flow_ab = flows(mergedcsv, group_a, group_b)

这导致KeyError:[[189,197,198,201]]中没有[[],<栏]&#39;

我尝试过使用ward_lista = map(str,group_a)和map(int,group_a),但列出了在dataframe.loc中找不到的对象。 列是混合数据类型,但包含应切片的标签的所有列都是int64类型。 我已尝试过围绕数据类型的许多解决方案,但无济于事。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

我无法谈论您正在进行的计算,但似乎您只想安排组合。问题是它们是指向还是未指向 - 也就是说,您是否需要计算流量(A,B)和流量(B,A),还是仅需要一个?

如果只有一个,你可以这样做:

for i,ward_list in enumerate(idgroups):
    for j,ward_list2 in enumerate(idgroups[i:],start=i):

这会迭代i,j对,如:

0,0 0,1 0,2 ... 0,n
1,1 1,2 ... 1,n
2,2 ... 2,n

将在无向案件中发挥作用。

如果你需要计算流量(A,B)和流量(B,A),那么你可以简单地将你的代码推送到一个名为flows的函数中,并用反向args调用它,如图所示。 ; - )

<强>更新

让我们定义一个名为flows的函数:

def flows():
    pass

现在,参数是什么?

好吧,看看你的代码,它从DataFrame中获取数据。你想要两个不同的病房,让我们从那些开始。结果似乎是结果网格的总和。

def flows(df, ward_a, ward_b):
    """Return the sum of all the cells in the row/column intersections
    of ward_a and ward_b."""

    return 0

现在我要复制你的代码行:

    ward_list = idgroups[index]
    print ward_list
    df6 = mergedcsv.loc[ward_list] #select rows with values in the list
    dfcols = mergedcsv.loc[ward_list, :] #select columns with values in list
    ward_liststr = map(str, ward_list) #convert ward_list to strings so that they can be used to select columns, won't work as integers.
    ward_listint = map(int, ward_list)
    #dfrowscols = mergedcsv.loc[ward_list, ward_listint]
    df7 = df6.loc[:, ward_liststr]
    print df7
    regflowsum = df7.values.sum() #sum all values in dataframe
    intflow = [regflowsum]
    print intflow

我认为这是flow功能的大部分内容。我们来看看。

  1. ward_list显然是ward_award_b参数。

  2. 我不确定df6是什么,因为您可以在df7中重新计算它。所以需要澄清。

  3. 我认为
  4. regflowsum是我们想要的输出。

  5. 将其重写为函数:

    def flows(df, ward_a, ward_b):
        """Return the sum of all the cells in the row/column intersections
        of ward_a and ward_b."""
    
        print "Computing flows from:"
        print "    ", ward_a
        print ""
        print "flows into:"
        print "    ", ward_b
    
        # Filter rows by ward_a, cols by ward_b:
        grid = df.loc[ward_a, ward_b]
    
        print "Grid:"
        print grid
    
        flowsum = grid.values.sum()
    
        print "Flows:", flowsum
    
        return flowsum
    

    现在,我假设ward_award_b值已经采用了正确的格式。所以我们必须str - ify他们或函数之外的任何东西。我们这样做:

    for ax, group_a in enumerate(idgroups):
        ward_a = map(str, group_a)
    
        for bx, group_b in enumerate(idgroups[ax:], start=ax):
            ward_b = map(str, group_b)
    
            flow_ab = flows(mergedcsv, ward_a, ward_b)
    
            if ax != bx:
                flow_ba = flows(mergedcsv, ward_b, ward_a)
            else:
                flow_ba = flow_ab
    
            # Now what?
    

    此时,您有两个号码。当病房相同时(内部流量?),它们将是平等的。此时您的原始代码不再有用,因为它只处理内部流,而不是A-> B流,所以我不知道该怎么做。但是这些值在变量中,所以......