python - 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.
解决方案
利用:
import ast
df['column_name']=ast.literal_eval(df['column_name'])
df['column_name'].str[0]
或者,如果类型是对象,您可以尝试:
df['column_name'].str.extract('(\[(.*?)\])',expand=False)
推荐阅读
- html - 引导网格系统未显示正确的列划分
- logging - 如何配置goaccess进行实时报告
- python - AutoField 没有减少列的长度
- pytest - 在 Vscode 中测试扩展。无法读取文件
- javascript - JavaScript - 以特定元素为目标,只知道它的类
- javascript - 焦点时下拉菜单不会消失
- swift - 我正在swift中寻找一个简单的splitView macOS模板,谁能提供一个方向?
- flutter - Flutter 我无法使用 SharedPreference 保存对象
- css - 无法从 material-ui TabPane 中删除填充
- javascript - 在axios中设置cookie不适用于跨域请求