首页 > 解决方案 > Convert list encasing inside data frame row values

问题描述

I have a Pandas Series that has list elements in each row. I want to de-list those values and store them back in the series. It looks like this. Can someone please help?

0                           ['Harvard business review']
1                           ['Harvard business review']
2     ['Journal of Medical Marketing: Device, Diagno...
3                           ['Harvard business review']
4                    ['Science Translational Medicine']
5                    ['Science Translational Medicine']
6                                       ['Tetrahedron']
7                                       ['Tetrahedron']
8                                           ['bioRxiv']
9                                           ['Science']
10                                     ['Cell Systems']
11                                       ['Prescriber']
12                                             ['Cell']

I tried to convert each into a list and then extract the values, but it would not convert to list.

标签: pythonpandasdataframe

解决方案


利用:

import ast
df['column_name']=ast.literal_eval(df['column_name'])
df['column_name'].str[0]

或者,如果类型是对象,您可以尝试:

df['column_name'].str.extract('(\[(.*?)\])',expand=False)

推荐阅读