user10963388
user10963388

Reputation:

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

Upvotes: 3

Views: 84

Answers (2)

DirtyBit
DirtyBit

Reputation: 16782

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

Upvotes: 1

jezrael
jezrael

Reputation: 863256

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

Upvotes: 0

Related Questions