通过python数组循环以匹配来自第二个数组的多个条件,快速方法?

通过python数组循环以匹配来自第二个数组的多个条件,快速方法?,python,arrays,excel,pandas,numpy,Python,Arrays,Excel,Pandas,Numpy,我是Python的初学者,我想知道是否有更快的方法来编写这段代码,请原谅我的无知。我有两张excel表格:一张(结果)有大约30000行的唯一用户ID,然后我有30列问题,下面的单元格是空的。我的第二张纸(答案),大约有400000行和3列。第一列有用户ID,第二列有被询问的问题,第三列有用户对每个相应问题的答案。我想做的是一个索引匹配数组Excel函数,在这里,我可以通过匹配用户ID和问题的答案,从表1中填空2个单元格中的空白单元格。p> 现在我写了一段代码,但只处理表1中的4列大约需要2

我是Python的初学者,我想知道是否有更快的方法来编写这段代码,请原谅我的无知。我有两张excel表格:一张(结果)有大约30000行的唯一用户ID,然后我有30列问题,下面的单元格是空的。我的第二张纸(答案),大约有400000行和3列。第一列有用户ID,第二列有被询问的问题,第三列有用户对每个相应问题的答案。我想做的是一个索引匹配数组Excel函数,在这里,我可以通过匹配用户ID和问题的答案,从表1中填空2个单元格中的空白单元格。p>

现在我写了一段代码,但只处理表1中的4列大约需要2个小时。我试图弄清楚我的方法是否没有完全利用Numpy功能

import pandas as pd
import numpy as np

# Need to take in data from 'answers' and merge it into the 'results' data
# Will requiring matching the data based on 'id' in column 1 of 'answers' and the
# 'question' in column 2 of 'answers'
results = pd.read_excel("/Users/data.xlsx", 'Results')
answers = pd.read_excel("/Users/data.xlsx", 'Answers')

answers_array = np.array(answers) #########

# Create a list of questions being asked that will be matched to column 2 in answers. 
# Just getting all the questions I want
column_headers = list(results.columns)
formula_headers = []              #########
for header in column_headers:
   formula_headers.append(header)
del formula_headers[0:13]

# Create an empty array with ids in which the 'merged' data will be fed into
pre_ids = np.array(results['Id'])
ids = np.reshape(pre_ids, (pre_ids.shape[0], 1))
ids = ids.astype(str)

zero_array = np.zeros((ids.shape[0], len(formula_headers)))
ids_array = np.hstack((ids, zero_array))    ##########


for header in range(len(formula_headers)):
    question_index = formula_headers[header]
    for user in range(ids_array.shape[0]):
        user_index = ids_array[user, 0]
        location = answers_array[(answers_array[:, 0] == int(user_index)) & (answers_array[:, 1] == question_index)]
        # This location formula is what I feel is messing everything up,
        # or could be because of the nested loops
        # If can't find the user id and question in the answers array
        if location.size == 0:
            ids_array[user][header + 1] = ''
        else:
            row_location_1 = np.where(np.all(answers_array == location[0], axis=1))
            row_location = int(row_location_1[0][0])
            ids_array[user][header + 1] = answers_array[row_location][2]

print ids_array

我们不必用第二个数据帧中的信息填充第一个数据帧,而只需旋转第二个数据帧

answers.set_index(['id', 'question']).answer.unstack()
如果需要使行和列与
结果
数据框中的行和列相同,可以添加
reindex\u like
方法

answers.set_index(['id', 'question']).answer.unstack().reindex_like(results)
如果你有重复的

cols = ['id', 'question']
answers.drop_duplicates(cols).set_index(cols).answer.unstack()

Hmm问题是答案表中的第1列有重复的用户ID来说明他们的答案question@MiriamAlh是的,这就是我在
id
question
@MiriamAlh上设置索引的原因。您有我可以演示的示例数据吗?谈论一个我看不见的数据集是非常困难的。我只是用一些工作表示例剪辑了我的问题。我尝试了您的代码,但返回了以下错误:ValueError:索引包含重复的条目,无法reshape@MiriamAlh这意味着您的答案数据框中有重复的id和问题组合。你希望这样吗?您希望如何处理重复项?保持第一?最后?没有一个