-2

I have a Panda data frame

    X =

      id                   var1  var2
 0   20000049588638         3    61.62
 1   100798486386           3    61.62
 2   100799238114           3    61.62

I want to convert this as a simple 2D array so that I can write this into Teradata database

Required Output

    X =   
    [(20000049588638,3,61.62),
    (100798486386,3,61.62), 
    (100799238114,3,61.62)]

I tried this:

    X = X.values.tolist()

But, I am getting following output:

    [[20000049588638, '3', '61.62'],
    [100798486386, '3', '61.62'],
    [100799238114, '3', '61.62']]

Which I am not able to write into the database.

Please check this.

1 Answers1

1

As mentioned in this questions, you can use itertuples() and then enclose that in a list.

list(X.itertuples(index=False, name=None))
razdi
  • 1,388
  • 15
  • 21
  • 3
    instead of writing the answer I guess you should mark the question as duplicate. – Amazing Things Around You Jun 04 '19 at 06:38
  • 1
    I had already written the answer before i saw you marking it. Once you did, I upvoted and I also added the link to the question. :) – razdi Jun 04 '19 at 06:40
  • Thanks @razdi. But, it's giving me following wrong output: [Pandas(id=20000049588638, var1='3', var2='61.62'), Pandas(id=100798486386, var1='3', var2='61.62'), Pandas(id=100799238114, var1='3', var2='61.62'),] – Mithilesh Kumar Jun 04 '19 at 06:52
  • That is cause they are named tuples. I've edited the answer and should give you the ordinary tuples. You should also look at the duplicate question as it has some more insights/answers – razdi Jun 04 '19 at 07:03