如何在变量中包含“for”循环输出.csv金融机构

2024-05-15 02:40:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我是python编程的初学者,正在尝试找出如何从代码中获得所需的输出。我在另一个for循环中有一个for循环。我的目标是导出一个.csv文件,其中除了包含与我的结果相对应的那些“for”循环变量的详细信息外,还包括我的代码的结果。我在想我怎样才能操纵到.csv为了得到我所需要的(请在下面找到我想要的)输出.csv文件)。你知道吗

楷书

df1 = [1, 2, 3, 4, 5]
df2 = [I, II, III]
for i in df1.index:
    for j in df2.index:
        ### bunch of calculations happening here resulted in output###
output.to_csv(r'd:\project\output.csv')

我想要的输出.csv文件

output     df1      df2
A          1        I
B          1        II
C          1        III
D          2        I
E          2        II
F          2        III
G          3        I
H          3        II
I          3        III
G          4        I
K          4        II
L          4        III
N          5        I
M          5        II
O          5        III

Tags: 文件csv代码in目标foroutputindex
1条回答
网友
1楼 · 发布于 2024-05-15 02:40:57

试试这个(熊猫):

import pandas as pd,string
l=[]
l2=[]
df1 = [1, 2, 3, 4, 5]
df2 = ['I', 'II', 'III']
for i in df1:
    for j in df2:
        l.append(i)
        l2.append(j)
output=pd.DataFrame({'output':string.ascii_uppercase[:len(l)],'df1':l,'df2':l2})
output.to_csv(r'd:\project\output.csv')

相关问题 更多 >

    热门问题