首页 > 解决方案 > pandas explode function only removes quotes

问题描述

I have a df that looks like this:

index |  entry_terms
0     | ['Abate', 'Difos', 'Temephos']
1     | []
2     | ['a', 'b']

I'm trying to run the .explode function the entry_terms column. Doing this straight on with df['entry_terms'].explode().to_frame() doesn't make any changes whatsoever. So I do some troubleshooting.

df.dtypes looks like this:

Unnamed: 0       int64
entry_terms     object
dtype: object

and the class of the entry_terms is <class 'str'>. Someone recommends converting into a list prior to running the explode function, so I run the following: df['entry_terms'].apply(literal_eval) followed by explode.to_frame. Still no explosion/change. Any help would be greatly appreciated!

标签: pythonpandas

解决方案


要使用 df.explode() 函数,您只需要传递一个参数:具有类似列表的值的列的名称。

df3 = df.explode('entry_terms').drop_duplicates()

推荐阅读