該cursor物件在 at 處執行查詢Oracle并使用fetchAll方法獲取結果,并且能夠將它們轉換為csv file.
但我想footer(<some static value>,<total number of records present in the dataframe>,<current date in DDMMYY format>)在檔案末尾添加如下所示:
代碼:
statement= 'select firstname,lastname,age,gender,college,university from students;'
cursor.execute(statement)
result_list = cursor.fetchall();
df = pd.DataFrame(result_list)
print(df)
輸出:
0 1 2 3 4 5
0 arun sai 25 M testcollege testuniversity
1 varun tej 28 F testcollege testuniversity
2 rachel green 27 M testcollege testuniversity
3 le blanc 25 M testcollege testuniversity
預期輸出:
0 1 2 3 4 5
0 arun sai 25 M testcollege testuniversity
1 varun tej 28 F testcollege testuniversity
2 rachel green 27 M testcollege testuniversity
3 le blanc 25 M testcollege testuniversity
4 ABC 4 011221
如何使用熊貓資料框實作它?
我嘗試了很多使用dataframe append和Dataframe.loc操作的方法但都無法實作。
當前的 csv 檔案:
arun,sai,25,M,testcollege,testuniversity
varun,tej,28,F,testcollege,testuniversity
rachel,green,27,M,testcollege,testuniversity
le,blanc,25,M,testcollege,testuniversity
預期的 csv 檔案:
arun,sai,25,M,testcollege,testuniversity
varun,tej,28,F,testcollege,testuniversity
rachel,green,27,M,testcollege,testuniversity
le,blanc,25,M,testcollege,testuniversity
ABC,4,071221
使用:
Python3.5
Pandas1.3.4
uj5u.com熱心網友回復:
這取決于您是否直接使用 DataFrame 將資料呈現給最終用戶——雖然它很容易使用,但我認為 DataFrame 更多的是關于資料操作,而不是關于資料展示。
我們目前實作這一目標的方法是不使用 DataFrame 呈現表格,而是使用另一個 HTML 層將最終表格呈現給用戶。
我們使用的框架稱為DataTables。但是,如果目前您只在 Jupyter Notebook 的世界中,那么這種方法將不起作用......
uj5u.com熱心網友回復:
畢竟分析,我認為在 Dataframe 上進行操作對于這種情況是不正確的,甚至也無法實作。
由于我從資料庫中獲得的結果記錄數量會很大,因此繼續使用 Pandas 只是為了將結果寫入 csv 檔案,但是使用 csv writer 在底部處理頁腳并將頁腳附加到最后一行。到目前為止,我可以看到這是唯一的解決方法,并期待社區提供更好的答案
# Pre-requisite - Import the writer class from the csv module
from csv import writer
# The data assigned to the list
list_data=['03','Smith','Science']
# Pre-requisite - The CSV file should be manually closed before running this code.
# First, open the old CSV file in append mode, hence mentioned as 'a'
# Then, for the CSV file, create a file object
with open('CSVFILE.csv', 'a', newline='') as f_object:
# Pass the CSV file object to the writer() function
writer_object = writer(f_object)
# Result - a writer object
# Pass the data in the list as an argument into the writerow() function
writer_object.writerow(list_data)
# Close the file object
f_object.close()
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/376303.html
