简体   繁体   English

如何使用pandas python附加for循环的所有输出

[英]How to append all output from for loop using pandas python

I have an excel file (df2) and i have used for loop to it to get multiple outputs and then i want to append all the multiple outputs obtained for that file so that i can get it into one single excel.我有一个 excel 文件 (df2),我已经使用 for 循环来获取多个输出,然后我想附加为该文件获得的所有多个输出,以便我可以将其放入一个 excel 中。 Please find my code below and suggest some ideas so that i can complete my code.请在下面找到我的代码并提出一些想法,以便我可以完成我的代码。

import os
from os import path
import pandas as pd

src = "C:\\ASPAIN-ORANGE\\test_wind3\\udc\\folder\\"

df = pd.read_excel('check_.xlsx',sheet_name='Align_pivot')
files = [i for i in os.listdir(src) if i.startswith("_Verification_") and path.isfile(path.join(src, i))]
for f in files:
    slice1 = 19
    file_slice = f[slice1:].replace(".csv", "")
    df1 = pd.read_csv(f)
    total_rows_df1 = len(df1.axes[0])
    df2 = df[df['MO'] == (file_slice)]
    total_rows_df2 = sum(df2.To_Align)
    print("filename : "+str(file_slice))
    print("Number of Rows_df1: "+str(total_rows_df1))
    print("Number of Rows_df2: "+str(total_rows_df2))
    if total_rows_df1 == total_rows_df2:
        print('True')
    else:
        print('False')
    df2.to_excel('output.xlsx', index=False, na_rep = 'NA', header = True)

1st Iteration output第一次迭代输出

2nd Iteration output第二次迭代输出

3rd Iteration output第三次迭代输出

and so on等等

Final appended Output最终附加输出

Your kind help would really be appreciated.您的帮助将不胜感激。

You can use DataFrame.append method (Append rows of other to the end of caller, returning a new object):您可以使用 DataFrame.append 方法(将其他行附加到调用者的末尾,返回一个新对象):

df = df.append(sheet, ignore_index=True)

Once all rows are added you can call to_excel method to write the excel.添加所有行后,您可以调用to_excel方法来编写 excel。

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.append.html https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.append.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM