Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (17.6k points)

I have manipulated some data using pandas and now I want to carry out a batch save back to the database. This requires me to convert the dataframe into an array of tuples, with each tuple corresponding to a "row" of the dataframe.

My DataFrame looks something like:

In [182]: data_set

Out[182]: 

  index data_date   data_1  data_2

0  14303 2012-02-17  24.75   25.03 

1  12009 2012-02-16  25.00   25.07 

2  11830 2012-02-15  24.99   25.15 

3  6274  2012-02-14  24.68   25.05 

4  2302  2012-02-13  24.62   24.77 

5  14085 2012-02-10  24.38   24.61 

I want to convert it to an array of tuples like:

[(datetime.date(2012,2,17),24.75,25.03),

(datetime.date(2012,2,16),25.00,25.07),

...etc. ]

Any suggestion on how I can efficiently do this?

1 Answer

0 votes
by (41.4k points)

 To convert it to array of tuples do this:

subset = data_set[['data_date', 'data_1', 'data_2']]

tuples = [tuple(x) for x in subset.values]

If you want to learn more about Pandas then visit this Python Course designed by the Industrial Experts.

 

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...