python-3.x - 如何将数据框中的多列从输出转换为excel?
问题描述
我正在尝试将输出提取到 excel 文件中
import pandas as pd
file1='excel1.xlsx'
file2='excel2.xlsx'
df1=pd.read_excel(file1)
df2=pd.read_excel(file2)
l=df2['col1'].tolist()
for row in range(len(l)):
for index,row1 in df1.iterrows():
l[row].replace(row1['emp'],row1['desc'])
out=[a.replace('','_') for a in l]
length=[len(o) for o in out]
final_df=pd.DataFrame(out,index=df2['col1'],columns=['desc']
final_df.to_csv('output.csv')
在上面的代码中,它只在 excel 中给出变量结果,但我也需要在 excel 中获得长度变量结果。
当我试图在列中给出长度变量时,它给出的列应该是 1 但给出 2。
final_df=pd.DataFrame(out,index=df2['col1'],columns=['desc','Length']
final_df.to_csv('output.csv')
任何人都可以请帮助如何在excel中获得变量和长度变量结果吗?
解决方案
... previous code section
out=[a.replace('','_') for a in l]
length=[len(o) for o in out]
final = {'desc': a, 'Length': length}
final_df=pd.DataFrame(final,
index=df2['col1']
final_df.to_csv('output.csv')
推荐阅读
- c# - 当我尝试将数据从网格视图填充到文本框时出现异常
- c++ - 检查迭代器属于哪个向量
- jmeter - JMeter,如何在一个参数中传递多个值
- java - 从Java中的文本文件创建二维数组
- django - Django REST Framework 不拦截 404 异常,输出 HTML 而不是 JSON
- amazon-web-services - 仅允许来自安全组的 AWS 特定 HTTP 和 HTTPS 请求
- c# - 显示警告和路径的“Windows”参考缺失
- enums - CAPL:如何在不同的枚举中拥有相同的枚举成员名称?
- angular - 为什么带有#字的ng-container首先在Angular中绘制?
- cucumber - 小黄瓜表 - 删除重复