1

I am new to pandas, and I would appreciate any help. I have a pandas dataframe that comes from csv file. The data contains 2 columns : dates and cashflows. Is it possible to convert these list into list comprehension with tuples inside the list? Here how my dataset looks like:

2021/07/15  4862.306832
2021/08/15  3474.465543
2021/09/15  7121.260118

The desired output is :

[(2021/07/15, 4862.306832),
(2021/08/15, 3474.465543),
(2021/09/15, 7121.260118)]
Camilla
  • 111
  • 10
  • Does this answer your question? [Pandas convert dataframe to array of tuples](https://stackoverflow.com/questions/9758450/pandas-convert-dataframe-to-array-of-tuples) – sandertjuh Jun 02 '21 at 19:36

1 Answers1

1

use apply with lambda function

data = {
    "date":["2021/07/15","2021/08/15","2021/09/15"],
    "value":["4862.306832","3474.465543","7121.260118"]
}

df = pd.DataFrame(data)
listt = df.apply(lambda x:(x["date"],x["value"]),1).tolist()

Output:

[('2021/07/15', '4862.306832'),
 ('2021/08/15', '3474.465543'),
 ('2021/09/15', '7121.260118')]
Muhammad Safwan
  • 869
  • 7
  • 12