Pandas groupby over list
<p>I have a pandas data frame in the following format:</p>

<pre><code>Arrival Departure Park Station Count 
      8        10    5   [1,2]     1
      5        12    6   [3,4]     1
      8        10    5   [1,2]     1
</code></pre>

<p>I want to groupby this data frame by arrival, departure, park and station but since station is a list, I am getting an error. The output should look like:</p>

<pre><code>    Arrival Departure Park Station Count 
        8        10    5   [1,2]     2
        5        12    6   [3,4]     1
</code></pre>

<p>Could you please let me know if there is any way to solve this issue?         </p>

<pre><code>import pandas as pd
df = pd.DataFrame({'arrival':[8,5,8], 'departure':[10,12,10], \
'park':[5,6,5], 'station':[[1,2], [3,4], [1,2]]})

df['arrival_station'] = df.station.apply(lambda x: x[0])
df['departure_station'] = df.station.apply(lambda x: x[1])
print df

   arrival  departure  park station  arrival_station  departure_station
0        8         10     5  [1, 2]                1                  2
1        5         12     6  [3, 4]                3                  4
2        8         10     5  [1, 2]                1                  2
</code></pre>

<p>Now your station data is free and you can groupby as normal. </p>

python list pandas dataframe