<p>很高兴在0.25.0上分享熊猫的新版本<code>explode</code></p>
<pre><code>s=data.explode().str.get('productID').dropna()
s
Out[91]:
1 14306.0
7 60974.0
7 72961.0
8 78818.0
8 86765.0
9 155707.0
10 54405.0
10 69562.0
dtype: float64
</code></pre>
<hr/>
<p>为那些不想更新<code>pandas</code>的人共享<a href="https://stackoverflow.com/questions/53218931/how-to-unnest-explode-a-column-in-a-pandas-dataframe/53218939#53218939">function</a></p>
<pre><code>unnesting(data.to_frame('pid'),['pid'],1)['pid'].str.get('productID').dropna()
Out[18]:
1 14306
7 60974
7 72961
8 78818
8 86765
9 155707
10 54405
10 69562
Name: pid, dtype: int64
</code></pre>
<hr/>
<pre><code>def unnesting(df, explode, axis):
if axis==1:
idx = df.index.repeat(df[explode[0]].str.len())
df1 = pd.concat([
pd.DataFrame({x: np.concatenate(df[x].values)}) for x in explode], axis=1)
df1.index = idx
return df1.join(df.drop(explode, 1), how='left')
else :
df1 = pd.concat([
pd.DataFrame(df[x].tolist(), index=df.index).add_prefix(x) for x in explode], axis=1)
return df1.join(df.drop(explode, 1), how='left')
</code></pre>