<p>您需要在<code>groupby</code>之后将<code>index</code>更改为<code>entities</code>,以处理列<code>entities</code>,并在<code>groupby</code>语句中按<code>list</code>-['industry','index']进行分组:</p>
<pre><code>df = df.groupby(['industry', 'index'])['entities'].apply(list).reset_index()
print (df)
industry index entities
0 cars 0 [[Norway, it], [Mercedes, they], [it, EV, its]]
1 nature 1 [[fox, it], [them, rabbits]]
2 nature 2 [[whale, it]]
</code></pre>
<p>如果需要不在嵌套列表中的最后一个值,因为通过<code>if-else</code>使用lambda函数,每个组只能有一个值:</p>
<pre><code>df1 = (df.groupby(['industry', 'index'])['entities']
.apply(lambda x: x.tolist() if len(x) != 1 else x.iat[0])
.reset_index())
print (df1)
industry index entities
0 cars 0 [[Norway, it], [Mercedes, they], [it, EV, its]]
1 nature 1 [[fox, it], [them, rabbits]]
2 nature 2 [whale, it]
</code></pre>
<p>编辑:</p>
<p>如果列<code>entities</code>中只有列表的字符串表示,则可以在上述解决方案之前通过<code>ast</code>模块将值转换为列表:</p>
<pre><code>print (type(df['entities'].iat[0]))
<class 'str'>
import ast
df['entities'] = df['entities'].apply(ast.literal_eval)
print (type(df['entities'].iat[0]))
<class 'list'>
</code></pre>