你好,Pythonic愛好者。Python 3 - 使用熊貓編寫從構造的字典到excel
我遇到了一個相當有趣的小問題,由於缺乏經驗我一直無法解決。我正在基於圖表數據庫中的一組答案構建Python中的字典,並且遇到了一個有趣的困境。 (我正在運行的Python 3
當一切塵埃落定,我收到下面的示例輸出在我的Excel文件(這是從0列,每個條目是行:
實際Excel格式:
0/{'RecordNo': 0}
1/{'Dept': 'DeptName'}
2/{'Option 1': 'Option1Value'}
3/{'Option 2': 'Option2Value'}
4/{'Question1': 'Answer1'}
5/{'Question2': 'Answer2'}
6/{'Question3': 'Answer3'}
等。
預計EXCEL格式:
0/Dept, Option 1, Option 2, Question 1, Question 2, Question 3
1/DeptName, Option1Value, Option2Value, Answer1, Answer2, Answer3
的字典的鍵應該是標題和值,每行的內容,但由於某種原因,它會將它寫爲鍵和值,當我使用以下輸出代碼時:
EXCEL WRITER CODE:
ReportDF = pd.DataFrame.from_dict(DomainDict)
WriteMe = pd.ExcelWriter('Filname.xlsx')
ReportDF.to_excel(WriteMe, 'Sheet1')
try:
WriteMe.save()
print('Save completed')
except:
print('Error in saving file')
要構建字典,我用下面的代碼: EDIT(刪除子除了字典項,因爲它是相同的,將被精簡成一個函數調用,一旦主要作品)。
詞典PREP CODE:
for Dept in Depts:
ABBR = Dept['dept.ABBR']
#print('Department: ' + ABBR)
Forests = getForestDomains(Quarter,ABBR)
for Forest in Forests:
DictEntryList = []
DictEntryList.append({'RecordNo': DomainCount})
DictEntryList.append({'Dept': ABBR})
ForestName = Forest['d.DomainName']
DictEntryList.append({'Forest ': ForestName})
DictEntryList.append({'Domain': ''})
AnswerEntryList = []
QList = getApplicableQuestions(str(SA))
for Question in QList:
FAnswer = ''
QDesc = Question['Question']
AnswerResult = getAnswerOfQuestionForDomainForQuarter(QDesc, ForestName, Quarter)
if AnswerResult:
for A in AnswerResult:
if(str(A['Answer']) != 'None'):
if(isinstance(A, numbers.Number)):
FAnswer = str(int(A['Answer']))
else:
FAnswer = str(A['Answer'])
else:
FAnswer = 'Unknown'
else:
print('GOBBLEGOBBLE')
FAnswer = 'Not recorded'
AnswerEntryList.append({QDesc: FAnswer})
for Entry in AnswerEntryList:
DictEntryList.append(Entry)
DomainDict[DomainCount] = DictEntryList
DomainCount+= 1
print('Ready to export')
如果有人可以幫助我讓我的數據導出到Excel內正確的格式,這將不勝感激。
編輯: 打印最終字典導出到Excel中:
{0: [{'RecordNo': 0}, {'Dept': 'Clothing'}, {'Forest ': 'my.forest'}, {'Domain': 'my.domain'}, {'Question1': 'Answer1'}, {'Question2': 'Answer2'}, {'Question3': 'Answer3'}], 1: [{...}]}
你的字典準備代碼的方式過於冗長。你能寫一個[最小,完整,可驗證的例子](https://stackoverflow.com/help/mcve),包括樣本數據嗎? – cmaher
我可以進一步分解它,但這是一個建立在嵌套條目上的字典。我會盡我所能分解它。我想我可能知道問題在哪裏,但我不知道如何解決問題。 –
@cmaher我刪除了額外的加(選2),因爲它是我在使它成爲一個功能,一旦我想通了,計劃選項1的幾乎一模一樣的副本是怎麼回事失控(我認爲它與方式做我「M追加到字典) –