2
我是一個初學Python的人,想知道是否有更快的方法來做這個代碼,所以請原諒我的無知。我有2個Excel工作表:其中一個(結果)擁有大約30,000行唯一用戶標識,然後我提出了30個問題列,下面的單元格爲空。我的第二張(回答),有大約400,000行和3列。第一列有用戶ID,第二欄有問題,第三欄有用戶對應的每個問題的答案。我想要做的事實質上是一個索引匹配數組excel函數,我可以通過匹配用戶標識和問題來填充表單1中的空白單元格以及來自表單2的答案。通過python數組循環以匹配第二個數組中的多個條件,快速方法?
現在我寫了一段代碼,但花了大約2個小時只處理從表1,我試圖找出4列,如果我做這件事的方式是不採取完整的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
嗯問題那就是答案頁中的第1列有重複的用戶ID來說明他們對每個問題的回答 –
@MiriamAlh是的,這就是爲什麼我在'id'上設置索引的原因和'question' – piRSquared
@MiriamAlh你有我可以證明的樣本數據嗎?談論我無法看到的數據集非常困難。 – piRSquared