简体   繁体   English

如何从数据框创建列表?

[英]How to create a list from the dataframe?

I have a dataframe with coordinates x0,y0,x1,y1. 我有一个坐标为x0,y0,x1,y1的数据框。 I am trying to get a list of the coordinate 我正在尝试获取坐标列表

datafrme is like- datafrme就像-

      x0     y0      x1      y1      x2      y2 ...
 1  179.0   77.0    186.0   93.0    165.0   91.0... 
 2  178.0   76.0    185.0   93.0    164.0   91.0...

desired output 期望的输出

 [  [ [179.0,77.0], [186.0,93.0], [165.0,91.0  ],...  ],
    [ [178.0,76.0 ],[185.0,93.0 ],[164.0,91.0 ] ,...  ]
 ]

I am trying to get a list of coordinates with row-wise 我正在尝试按行获取坐标列表

I have tried this 我已经试过了

df = pd.read_csv(path,header=None)
df.reset_index(drop=True, inplace=True)
data = df.values.tolist()

Thank you for any and all insight! 感谢您提供的所有见解!

[[[y['x0'], y['y0']], [y['x1'], y['y1']], [y['x2'], y['y2']]]for x,y in df.iterrows()]

df here will be your dataframe df这将是您的数据框

the output will look like the format you described 输出将类似于您描述的格式

 [  [ [179.0,77.0], [186.0,93.0], [165.0,91.0  ],...  ],
    [ [178.0,76.0 ],[185.0,93.0 ],[164.0,91.0 ] ,...  ]
 ]

Use reshape if performance is important: 如果性能很重要,请使用reshape

a = len(df.columns)
b = len(df)

L = df.values.reshape(b, a).reshape(b, a // 2, 2).tolist()
print (L)
[[[179.0, 77.0], [186.0, 93.0], [165.0, 91.0]], 
 [[178.0, 76.0], [185.0, 93.0], [164.0, 91.0]]]

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

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