0

I have pandas to extract the data from excel files and adding them to a dataframe using the following code

flow_df_temp = {'date': date_fmt_csv, 'point_id': point_id_list_flow,'water_flow': water_list_flow}
flow_df = flow_df.append(flow_df_temp, ignore_index=True)`

Output that I get.

         date   point_id                  aggregated_flow
0  10/02/2022  [7, 8, 9]   [2.353, 55.51, 28.792]
1  10/01/2022  [7, 8, 9]  [6.281, 53.444, 23.371]

The following is the output that I want.

      date   point_id                  aggregated_flow
10/02/2022      7                          2.353
10/02/2022      8                          55.51
10/02/2022      9                          28.792
10/01/2022      7                          6.281
10/01/2022      8                          53.444
10/02/2022      9                          23.371 

I have tried to run some loops but not been able to figure out the logic.

Any update will be much appreciated.

pavan
  • 5
  • 2
  • [```explode```](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.explode.html) it :) – sophocles Oct 20 '22 at 13:56

1 Answers1

1

assuming that the point_id and aggreggated_flow are lists, and element counts are same for all rows. Here is how you make those lists turn into their own rows

df.explode(['point_id','aggregated_flow'])

        date        point_id    aggregated_flow
    0   10/02/2022         7     2.353
    0   10/02/2022         8    55.51
    0   10/02/2022         9    28.792
    1   10/01/2022         7     6.281
    1   10/01/2022         8    53.444
    1   10/01/2022         9    23.371

Naveed
  • 11,495
  • 2
  • 14
  • 21