[英]How to add a footer(row) to the dataframe with different column length in python?
The cursor
object executes query at Oracle
and gets the result using fetchAll
method, and am able to convert them into csv file
. cursor
object 在Oracle
处执行查询并使用fetchAll
方法获取结果,并能够将其转换为csv file
But I want to add footer(<some static value>,<total number of records present in the dataframe>,<current date in DDMMYY format>)
at end of the file like below:但我想在文件末尾添加
footer(<some static value>,<total number of records present in the dataframe>,<current date in DDMMYY format>)
如下:
code:
代码:
statement= 'select firstname,lastname,age,gender,college,university from students;'
cursor.execute(statement)
result_list = cursor.fetchall();
df = pd.DataFrame(result_list)
print(df)
output:
output:
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
Expected Output:
预期 Output:
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
How to achieve it with pandas dataframe?如何用 pandas dataframe 实现它?
I have tried many ways using dataframe append
and Dataframe.loc
operation but not able to achieve.我已经尝试了很多方法使用
dataframe append
和Dataframe.loc
操作但无法实现。
Current csv file:
当前 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
Expected csv file:
预期的 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
Using:使用:
Python3.5
Pandas1.3.4
It depends on whether or not you are directly using DataFrame to present the data to the end-users--while it is easy to used, I would argue that DataFrame is more about data manipulation and less about data presentation.这取决于您是否直接使用 DataFrame 将数据呈现给最终用户——虽然它易于使用,但我认为 DataFrame 更多的是关于数据操作而不是关于数据呈现。
The current way we achieve this is to not presenting the table with DataFrame and instead we use another HTML layer to present the final table to users.我们实现这一点的当前方法是不使用 DataFrame 呈现表格,而是使用另一个 HTML 层向用户呈现最终表格。
The framework we are using is called DataTables .我们使用的框架称为DataTables 。 However, if currently you are only in the Jupyter Notebook's world, then this approach would not work...
但是,如果当前您只在 Jupyter Notebook 的世界中,那么这种方法将行不通......
After all analysis, I believe doing manipulation at Dataframe is not correct for this scenarion even could not achieve it too.经过所有分析,我相信在 Dataframe 进行操作对于这种情况是不正确的,甚至无法实现。
since my result records from the database will be large in number, going ahead with pandas only to write the result into a csv file, but for the footer handling at bottom using csv writer and appending the footer at last row.由于我从数据库中获取的结果记录数量很大,因此继续使用 pandas 仅将结果写入 csv 文件,但是对于底部的页脚处理,使用 csv 并在最后一行添加页脚编写器。 As of now I can see this is the only work around and expecting for a better answer from community
到目前为止,我可以看到这是唯一的解决方法,并期待社区提供更好的答案
# 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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.