将3个不同For循环的结果显示到选项卡中

2024-05-15 03:32:42 发布

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

我想以表格格式并排显示所有三个列表以及与值关联的名称。我正在手动做它现在和它需要一段时间为所有20个文件,我必须做。谢谢你的帮助!你知道吗

  maxpreandpost = [Pre1,Pre2,Pre3,Pre4,Pre5,Pre6,Pre7,Pre8,Pre9,Pre10,Post1,Post2,Post3,Post4,Post5,Post6,Post7,Post8,Post9,Post10]
    for i in maxpreandpost:
        height = max(i.Z)
        print (height)

165.387美元 160.214 159.118 186.685 163.744 160.717 184.026 171.25099999999995 175.73 156.512 150.339 131.528 148.52100000000004 126.738 136.389 148.334 129.855 153.599 144.595 159.3229999995

lenpreandpost = [Pre1,Pre2,Pre3,Pre4,Pre5,Pre6,Pre7,Pre8,Pre9,Pre10,Post1,Post2,Post3,Post4,Post5,Post6,Post7,Post8,Post9,Post10]
for i in lenpreandpost:
    duration = len(i.Z)
    print (duration)

690个 543 292 271 293 147 209 355 230 293 395 256 349 255 335 255 231 243 315 267个

dis = [Pre1,Pre2,Pre3,Pre4,Pre5,Pre6,Pre7,Pre8,Pre9,Pre10,Post1,Post2,Post3,Post4,Post5,Post6,Post7,Post8,Post9,Post10]
for i in dis:
    p1 = [max(i.X),max(i.Y)]
    p2 = [min(i.X),min(i.Y)]
    distance = math.sqrt(((p1[0]-p2[0])**2)+((p1[1]-p2[1])**2)) 
    print (distance)

2219.0546989150585 2337.434842606099 1857.1832474809803 1450.0472277998394 1512.6539831504758 1058.5635689541748 1653.517987682021 1854.670452561212 1861.8190476064021 1775.672511965326 1872.275393720069 1814.9932559772114 1852.3299779009246 1875.2281201398403 1867.1599096301313 1708.250531327712 1793.8521780715407 1862.7949271803914 1872.843665022548 1800.2239125453254


Tags: post2post4post1post3pre2post5pre4pre1
1条回答
网友
1楼 · 发布于 2024-05-15 03:32:42

当然,将所有值附加到输出列表,然后将它们添加到数据帧:

import pandas as pd

heightmax = []
maxpreandpost = [Pre1,Pre2,Pre3,Pre4,Pre5,Pre6,Pre7,Pre8,Pre9,Pre10,Post1,Post2,Post3,Post4,Post5,Post6,Post7,Post8,Post9,Post10]
for i in maxpreandpost:
    height = max(i.Z)
    heightmax.append(height)

duration_pre_post = []
lenpreandpost = [Pre1,Pre2,Pre3,Pre4,Pre5,Pre6,Pre7,Pre8,Pre9,Pre10,Post1,Post2,Post3,Post4,Post5,Post6,Post7,Post8,Post9,Post10]
for i in lenpreandpost:
    duration = len(i.Z)
    duration_pre_post.append(duration)

dis_p1_p2 = []
dis = [Pre1,Pre2,Pre3,Pre4,Pre5,Pre6,Pre7,Pre8,Pre9,Pre10,Post1,Post2,Post3,Post4,Post5,Post6,Post7,Post8,Post9,Post10]
for i in dis:
    p1 = [max(i.X),max(i.Y)]
    p2 = [min(i.X),min(i.Y)]
    distance = math.sqrt(((p1[0]-p2[0])**2)+((p1[1]-p2[1])**2)) 
    dis_p1_p2.append(distance)

df = pd.DataFrame() # initialize empty dataframe

# Store each list as a column in the df.
df['HeightMax'] = heightmax
df['DurationPrePost'] = duration_pre_post
df['DistanceP1P2'] = dis_p1_p2

#if you want to write this out to a tabular file:

df.to_csv('./Desktop/myDf.csv', sep='\t', index=False)

其输出类似于:

  HeightMax    DurationPrePost    DistanceP1P2
0  165.387          690           2219.0546989150585
1  160.214          543           2337.434842606099
2  159.118          292           1857.1832474809803 
3  186.685          271           1450.0472277998394
4  163.744          293           1512.6539831504758
... #extends to end of lists

相关问题 更多 >

    热门问题