python pandas: diff between 2 dates in a groupby - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: python pandas: diff between 2 dates in a groupby (/thread-25281.html) |
python pandas: diff between 2 dates in a groupby - bluedragon - Mar-25-2020 Hi all, I'm trying to implement this example: import pandas as pd import io df = pd.read_csv(io.StringIO('''transactionid;event;datetime;info 1;START;2017-04-01 00:00:00; 1;END;2017-04-01 00:00:02;foo1 2;START;2017-04-01 00:00:02; 3;START;2017-04-01 00:00:02; 2;END;2017-04-01 00:00:03;foo2 4;START;2017-04-01 00:00:03; 3;END;2017-04-01 00:00:03;foo3 4;END;2017-04-01 00:00:04;foo4'''), sep=';', parse_dates=['datetime']) df.datetime = pd.to_datetime(df.datetime) funcs = { 'datetime':{ 'start_date': 'min', 'end_date': 'max', 'duration': lambda x: x.max() - x.min(), }, 'info': 'last' } df.groupby(by='transactionid')['datetime','info'].agg(funcs).reset_index() print(df)The expected output should be: Using python3.7 and I'm getting the following error: Any idea to solve this issue?Thanks a lot |