Camilla
Camilla

Reputation: 131

Creating List Comprehension using pandas dataframe

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)]

Upvotes: 1

Views: 300

Answers (1)

Muhammad Safwan
Muhammad Safwan

Reputation: 1024

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')]

Upvotes: 1

Related Questions