pandas - create dataframe with counts and frequency of elements
<p>Starting from the following dataframe <code>df</code>:</p>

<pre><code>df = pd.DataFrame({'node':[1,2,3,3,3,5,5],'lang':['it','en','ar','ar','es','uz','es']})
</code></pre>

<p>I'm trying to build the structure:</p>

<pre><code>    node     langs   lfreq
0      1      [it]     [1]
1      2      [en]     [1]
2      3  [ar, es]  [2, 1]
3      5  [uz, es]  [1, 1]
</code></pre>

<p>so basically grouping the <code>lang</code> elements and frequency per node into a single row through lists. What I've done so far:</p>

<pre><code># Getting the unique langs / node
a = df.groupby('node')['lang'].unique().reset_index(name='langs')

# Getting the frequency of lang / node
b = df.groupby('node')['lang'].value_counts().reset_index(name='lfreq')
c = b.groupby('node')['lfreq'].unique().reset_index(name='lfreq')
</code></pre>

<p>and then merge on <code>node</code>:</p>

<pre><code>d = pd.merge(a,c,on='node')
</code></pre>

<p>After this operations, what I obtained is:</p>

<pre><code>    node     langs   lfreq
0      1      [it]     [1]
1      2      [en]     [1]
2      3  [ar, es]  [2, 1]
3      5  [uz, es]     [1]
</code></pre>

<p>As you may notice, the last row has only one <code>[1]</code> occurrence of the frequency of the two <code>[uz, es]</code> instead of a list of <code>[1,1]</code> as expected. Is there a way to perform the analysis in a more concise way obtaining the desired output?</p>