简体   繁体   English

如何使用 Python Z251D2BBFE9A3B6784AZ5691CEB3 将 append 少数数据导入 excel 文件

[英]How to append the few datas into excel file using Python Pandas

I am completely new to Python and struggling to sort out many issues faced while writing code.我对 Python 完全陌生,并且努力解决编写代码时面临的许多问题。 I have generated a series of results from the CSV file after converting it into an excel file.在将 CSV 文件转换为 excel 文件后,我生成了一系列结果。 Each file has produced a series of data.每个文件都产生了一系列数据。 For instance, CSV file no.1 gives the output U1, V1, W1, X1, and file no 2 gives U2, V2, W2, X2, and so on.例如,CSV 文件 1 给出了 output U1、V1、W1、X1,文件 2 给出了 U2、V2、W2、X2 等。 I want to export these files into an excel file as a number of rows.我想将这些文件作为多行导出到 excel 文件中。 I made a dataframe to incorporate U, V, W, X & exported it to excel using 'to_Excel'.我制作了一个 dataframe 以合并 U、V、W、X 并使用“to_Excel”将其导出到 excel。 Unfortunately, I am getting only results from one file.不幸的是,我只从一个文件中得到结果。 You can see it from my attached screenshot enter image description here , which shows the excel file and the real output.您可以从我附加的屏幕截图中看到它在此处输入图像描述,其中显示了 excel 文件和真正的 output。 Can anyone help to export all the results into excel in several rows (not on different sheets), rather than just the analysis from one CSV file?任何人都可以帮助将所有结果导出到 excel 中的几行(不在不同的工作表上),而不仅仅是来自一个 CSV 文件的分析? Also, I need to add one more piece of information to the final excel sheet, which is the excel file name.另外,我需要在最终的 excel 表中添加一条信息,即 excel 文件名。

I am seeking suggestions to solve the above issue.我正在寻求解决上述问题的建议。 FOLLOWING IS MY SECTION OF PYTHON CODE RELATED TO THE EXPORT OF RESULTS TO AN EXCEL FILE以下是我与将结果导出到 EXCEL 文件相关的 PYTHON 代码部分

for excel_file in glob(r"C:\Users\sujith0327\pythonProject2\0 Nano Attempts*.xlsx"):对于 glob 中的 excel_file(r"C:\Users\sujith0327\pythonProject2\0 Nano Attempts*.xlsx"):

df_excel = pd.read_excel(excel_file)


max_depth_no = df_excel['Depth(nm)'].idxmax()


loading_part = df_excel.iloc[0:max_depth_no]
unloading_part = df_excel.iloc[max_depth_no:-1]


y = np.array(loading_part['Load (µN)'])
area_load = trapz(y, dx=0.01)


y = np.array(unloading_part['Load (µN)'])
area_unload = trapz(y, dx=0.01)


y = np.array(df_excel['Load (µN)'])
area = trapz(y, dx=0.01)


reversible_area = area_load - area_unload
irreversible_area = area_unload
total_area = reversible_area + irreversible_area

reversible_share = (reversible_area / total_area) * 100
irriversible_share = (irreversible_area / total_area) * 100



max_depth = df_excel['Depth(nm)'][max_depth_no]
remnant_depth = df_excel['Depth(nm)'].iat[-1]
max_load = df_excel['Load (µN)'][max_depth_no]
depth_recoverability = (1 - (remnant_depth / max_depth)) * 100

print(max_depth, remnant_depth, max_load, depth_recoverability, reversible_share, irriversible_share)

data = {'Max Load ((µN)': [max_load], 'Max Depth (nm)': [max_depth], 'Remnant Depth(nm)': [remnant_depth],
        'Depth Recoverability (%)': [depth_recoverability], 'Reversible Energy (%)': [reversible_share],
        'Irriversible Energy (%)': [irriversible_share]}
frame = pd.DataFrame(data)
print(data)

frame.to_excel("00 ALL RESULTS.xlsx") frame.to_excel("00 所有结果.xlsx")

when saving to excel use this code保存到 excel 时使用此代码

writer = pd.ExcelWriter(path, engine = 'xlsxwriter')
df1.to_excel(writer, sheet_name = 'x1')
df2.to_excel(writer, sheet_name = 'x2')
writer.save()
writer.close()

check this answer: https://stackoverflow.com/a/42375263/12009526检查这个答案: https://stackoverflow.com/a/42375263/12009526

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

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