1

I have a following problem. My database returns a list:

[Order(id=22617, frm=datetime.datetime(2020, 6, 1, 8, 0), to=datetime.datetime(2020, 6, 1, 10, 0), loc=Location(lat=14.491272455461, lng=50.130463596998), address='Makedonska 619/11, Praha', duration=600), datetime.datetime(2020, 6, 1, 11, 38, 46), Order(id=22615, frm=datetime.datetime(2020, 6, 1, 8, 0), to=datetime.datetime(2020, 6, 1, 14, 0), loc=Location(lat=14.681866313487, lng=50.007439571346), address='Výhledová 256, Říčany', duration=600), datetime.datetime(2020, 6, 1, 10, 33, 33)]

Every output from the database is a type routes_data_loading.data_structures.Order and datetime.datetime. I would like to save it as a pandas dataframe.

Desired output for the first row is:

id;frm;to;lat;lng;address;duration;time
22617;2020-06-01 08:00;2020-06-01 10:00;14.491272455461;50.130463596998;Makedonska 619/11, Praha;600;2020-06-01 11:38:46

Semicolumn stands for a new column. Note that the last column time has to be created, because its name is not in the original list.

Can you help me how to convert this list into pandas df, please? I know how to convert simple list into df, but not this complicated one. Thanks a lot.

1
  • 1
    Try if lst[0].__dict__ return something. Commented Oct 11, 2021 at 13:45

1 Answer 1

2

Try this without guarantee of success:

data = []
for order, time in zip(lst[::2], lst[1::2]):
    data.append({'id': order.id, 'frm': order.frm, 'to': order.to,
                 'lat': order.loc.lat, 'lng': order.loc.lng,
                 'address': order.address, 'duration': order.duration, 
                 'time': time})

df = pd.DataFrame(data)

Output:

>>> df
      id                 frm                  to        lat        lng                   address  duration                time
0  22617 2020-06-01 08:00:00 2020-06-01 10:00:00  14.491272  50.130464  Makedonska 619/11, Praha       600 2020-06-01 11:38:46
1  22615 2020-06-01 08:00:00 2020-06-01 14:00:00  14.681866  50.007440     Výhledová 256, Říčany       600 2020-06-01 10:33:33

How do I setup:

from collections import namedtuple
import datetime

Order = namedtuple('Order', ['id', 'frm', 'to', 'loc', 'address', 'duration'])
Location = namedtuple('Location', ['lat', 'lng'])

lst = [Order(id=22617, frm=datetime.datetime(2020, 6, 1, 8, 0), to=datetime.datetime(2020, 6, 1, 10, 0), loc=Location(lat=14.491272455461, lng=50.130463596998), address='Makedonska 619/11, Praha', duration=600),
       datetime.datetime(2020, 6, 1, 11, 38, 46),
       Order(id=22615, frm=datetime.datetime(2020, 6, 1, 8, 0), to=datetime.datetime(2020, 6, 1, 14, 0), loc=Location(lat=14.681866313487, lng=50.007439571346), address='Výhledová 256, Říčany', duration=600),
       datetime.datetime(2020, 6, 1, 10, 33, 33)]
Sign up to request clarification or add additional context in comments.

2 Comments

works fine, thanks!
Wow. Glad to read that. I was not confidence in my answer :) Thanks for your support!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.