[英]How to give comma separated values a new column in csv with pandas?
I'm setting up a new tool, and want to support my professor to extract topics of relevant patent data. 我正在设置一个新工具,并希望支持我的教授提取相关专利数据的主题。 I have used pandas to create a csv-file with the output of the analyzing tool.
我已经使用熊猫通过分析工具的输出创建了一个csv文件。 Actually, I have this code:
实际上,我有以下代码:
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=';')
I expect to get a csv-file where the topic labels are listed under the header "topic". 我希望得到一个csv文件,其中标题标签在标题“ topic”下列出。 It should look like this:
它看起来应该像这样:
; topic
0; Machine
1; Stairs
2; xxx
3; yyy
[...]
But the actual output is a csv-file where all topics are listed in one big column, like this: 但是实际输出是一个csv文件,其中所有主题都列在一个大列中,如下所示:
; topic
0; 'Machine', 'Stairs', 'xxx', 'yyy'
1; 'Machine', 'Stairs', 'xxx', 'yyy'
2; 'Machine', 'Stairs', 'xxx', 'yyy'
3; 'Machine', 'Stairs', 'xxx', 'yyy'
[...]
I'm thankful for your answers! 感谢您的回答!
您需要将转换集列出并删除[]
:
df = pd.DataFrame({'topic' : list(topics)})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.