简体   繁体   中英

How to create a list from the dataframe?

I have a dataframe with coordinates x0,y0,x1,y1. I am trying to get a list of the coordinate

datafrme is like-

      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

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:

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]]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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