简体   繁体   中英

how to convert lists of list to particular dataframe in python

here is my list , i want to convert it into a Dataframe

list =[["32:31",1,56],
      ["25:31",2,78],
      ["08:31",3,3],
      ["28:41",4,98]]

can we convert this list to Dataframe like -: (i want to fetch third element only)

   a
  56
  78
   3
  98

i tried df = pd.DataFrame(list) with some condition but it didn't work out

please help me

thanks

import pandas as pd

_list =[["32:31",1,56],
      ["25:31",2,78],
      ["08:31",3,3],
      ["28:41",4,98]]

df = pd.DataFrame({'a':[elem[2] for elem in _list]})    
print(df)

OUTPUT:

    a
0  56
1  78
2   3
3  98

Dont use variable list , because python code word.

Use DataFrame contructor with list comprehension for get 3. value by indexing [2] (python count from 0 ):

L =[["32:31",1,56],
    ["25:31",2,78],
    ["08:31",3,3],
    ["28:41",4,98]]

df = pd.DataFrame({'a':[x[2] for x in L]})
print (df)
    a
0  56
1  78
2   3
3  98

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