-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
  • you can also use: df.apply(tuple,axis=1).to_numpy() Commented Jun 4, 2019 at 6:48

1 Answer 1

1

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

list(X.itertuples(index=False, name=None))
Sign up to request clarification or add additional context in comments.

4 Comments

instead of writing the answer I guess you should mark the question as duplicate.
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. :)
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'),]
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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.