我正在设置一个新工具,并希望支持我的教授提取相关专利数据的主题。我使用 pandas 创建了一个带有分析工具输出的 csv 文件。实际上,我有这段代码:
import textrazor
textrazor.api_key= 'b033067632dba8a710c57f088115ad4eeff22142629bb1c07c780a10'
csv_contents = open('Patentdaten1.csv').read()
client = textrazor.TextRazor(extractors=['topics', 'entities'])
response = client.analyze(csv_contents)
topics =set()
relevance =set()
topics1= list(response.topics())
topics1.sort (key=lambda x:x.score, reverse=True)
for topic in response.topics():
if topic.score > 0.5:
if topic.label not in topics:
topics.add(topic.label)
relevance.add(topic.score)
import pandas as pd
df = pd.DataFrame({'topic' : [topics]})
df.to_csv('Test.csv', sep=';')
我希望获得一个 csv 文件,其中主题标签列在标题“主题”下。 它应该看起来像这样:
; topic
0; Machine
1; Stairs
2; xxx
3; yyy
[...]
但实际输出是一个 csv 文件,其中所有主题都列在一个大列中,如下所示:
; topic
0; 'Machine', 'Stairs', 'xxx', 'yyy'
1; 'Machine', 'Stairs', 'xxx', 'yyy'
2; 'Machine', 'Stairs', 'xxx', 'yyy'
3; 'Machine', 'Stairs', 'xxx', 'yyy'
[...]
非常感谢您的回答!
最佳答案
您需要转换集以列出并删除[]
:
df = pd.DataFrame({'topic' : list(topics)})
关于python - 如何使用 pandas 在 csv 中为逗号分隔值提供新列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57674296/