While循环中,输出结果只有最后一次运算的结果,如何提取合并过程中所有结果呢?

weixin_46117650 2019-12-30 04:29:33
import pandas as pd
import re
import collections


excelFile = ‘2.xlsx'
df = pd.DataFrame(pd.read_excel(excelFile))
cblist=[]
df1=df['Object description'].dropna(how='all')
for name in df1:
if '10' in name:
cblist.append(name)
df3=pd.DataFrame
df4=pd.DataFrame
list=[]
a=0
while a<(len(cblist)):
for op in cblist[a]:
df2=df.loc[df['Object description'] == cblist[a]]
cablelist2 = df2[['Object ID']].values.T.tolist()[:][0]
for op in cablelist2:
dfByID = df.loc[df['under Rev. Parts no.'] == op]
print('\r\n')
df3 = df2.append(dfByID)
sorted_df3 = df3.sort_index()
df4 = sorted_df3.update
print(sorted_df3)
break
a += 1


期望输出的结果如下,本结果还希望写入excel表格,因此如何将所有sorted_df3 的结果集合起来呢
Sort Lev under Rev. Parts no. ... Net Gross Quantity.1
20 4074 ...3 10263305 ... 1.017 1.017 1
21 4075 ....4 10259566 ... 0.000 0.000 1
22 4076 ....4 10259566 ... 0.005 0.005 5
23 4077 ....4 10259566 ... 0.005 0.005 5
24 4078 ....4 10259566 ... 0.001 0.001 1

[5 rows x 36 columns]


Sort Lev under Rev. Parts no. ... Net Gross Quantity.1
25 4131 ...3 10263305 ... 1.03 1.03 1
26 4132 ....4 10259573 ... 0.00 0.00 1

[2 rows x 36 columns]


目前结果只能输出最后一个结果


append 这个指令没办法用 因为每次输出的sorted_df3是不一样的但是名字是一样的。无法定义每次输出结果的名字。

感谢阅读

...全文
765 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
大河哗啦啦 2020-10-02
  • 打赏
  • 举报
回复
楼主问题解决了没?我也是刚学python,在做循环的时候想要输出这多次循环的全部结果,但多番尝试,输出总是最后一次的结果
  • 举报
回复
@大河哗啦啦 得我是想只要最终结果结果他都给了
weixin_46117650 2019-12-31
  • 打赏
  • 举报
回复

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧