如何用pandas读取逗号、分隔字符串到列表中并存储在neo4j中

问题描述 投票:0回答:1

我有一张 Excel 工作表,其中一个单元格可以包含一个以逗号分隔的字符串,例如

foo, bar, baz
。 我想用 pandas 读取它并将其存储为列表,然后该列表应该存储在 neo4j 数据库中。 但无论我尝试什么,我总是得到这样的字符串:
"['foo', 'bar', 'baz']"
但我需要 `["foo", "bar", "baz"]

因此,

read_excel
只为其他列提供一些转换器。此处设置
list
会导致错误,即列表不可散列。
dtypes
也不会接受
list
:-/

知道如何继续吗? 我也尝试过以某种方式使用这个解决方案,但没有成功: 如何从Excel单元格读取列表


@mozway 根据要求,

to_dict('tight')
输出:

{'index': [0, 1, 2], 'columns': ['name', 'alternative_names', 'description'], 'data': [['test_service1', 'ts1, xs1, zs1', 'This is a description'], ['test_service2', 'ts2', 'This is a description'], ['test_service3', 'ts3', 'This is a description']], 'index_names': [None], 'column_names': [None]}
python pandas excel list neo4j
1个回答
0
投票

作为解决方法,您可以将“['foo', 'bar', 'baz']”之类的字符串按原样发送到 neo4j Cypher 查询,并使用 Cypher 将其转换为字符串列表。例如,如果您将此类“原始”字符串传递到 $raw parameter 中的 Cypher 查询:

WITH SPLIT(REPLACE(SUBSTRING($raw, 1, SIZE($raw)-2), "'", ""), ', ') AS my_list
...
© www.soinside.com 2019 - 2024. All rights reserved.