3

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

1
  • df = pd.DataFrame([e[2] for e in list])? Also do not use list as variable name, it shadows the built-in name list. Commented Feb 7, 2019 at 14:02

2 Answers 2

1
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
Sign up to request clarification or add additional context in comments.

Comments

0

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

3 Comments

@AmitGupta - yes, it was answered after me... check times.
hey, this is just curiosity question, is this possible without iteration? because i have very huge amount of data(means list size is too large)
Yes, use df = pd.DataFrame({'a':list(zip(*L))[2]})

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.