简体   繁体   English

如何在 python 中向 dataframe 中添加不同列长度的页脚(行)?

[英]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 appendDataframe.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.

相关问题 Python:将一列添加到 dataframe 中,具有不同的长度重复添加的列直到填充 dataframe 长度 - Python : Add a column into a dataframe with different length repeating the added column till fill the dataframe length Python-将numpy数组作为列添加到具有不同长度的pandas数据帧 - Python - add a numpy array as column to a pandas dataframe with different length 将列向量添加到不同长度的 dataframe - Add column vector to a dataframe of different length 在Python中读取具有不同页脚行长的.csv文件 - Reading .csv files with different footer row length in Python 我如何根据另一个具有不同长度但共享列数据的 dataframe 的条件向 dataframe 添加一列 - How can i add a column to a dataframe based on a conditional of another dataframe that has a different length, but shared column data 如何将 dataframe 中的每一行乘以不同 dataframe 的不同列,并将所有行的总和作为 Python 中的新列? - How to multiply each row in dataframe by a different column of different dataframe and get sum of all rows as a new column in Python? 如何打开具有不同长度元组的 dataframe 列? - How to unpack a dataframe column with tuples of different length? 在 PySpark 数据框中添加不同长度的列作为新列 - Add column with different length as new column in PySpark dataframe 在 dataframe 如何将带有列表的列(所有行的长度相同)分解为同一行的不同列 - At a dataframe how to explode a column with a list (with same length at all rows) into different columns at the same row 如何设置限制 python dataframe 列长度? - How to set limit to python dataframe column length?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM