简体   繁体   English

如何用逗号在CSV中给逗号分隔的值添加一个新列?

[英]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.

相关问题 Python Pandas为逗号分隔的值提供新列 - Python pandas give comma separated values new column 如何在新的列熊猫数据框中获取逗号分隔的值? - How to get comma separated values in new column pandas dataframe? pandas 合并列以使用逗号分隔值创建新列 - pandas merge columns to create new column with comma separated values 如果列中包含逗号分隔的元素,如何将逗号分隔的 `csv` 文件读入 pandas dataframe? - How to read a comma separated `csv` file into pandas dataframe if it contains a comma separated elements in a column? 如何为熊猫中的列中的每个逗号分隔值创建一个新行 - How to create a new row for each comma separated value in a column in pandas 如何在 pandas 的单个列中合并(逗号分隔的)行值? - How to combine (comma-separated) row values in a single column in pandas? python pandas 将逗号分隔的值放入带有“标题”的列中 - python pandas give comma separated values into columns with “title” Pandas 删除逗号分隔列值中的特定 int 值 - Pandas remove particular int values in comma separated column values 包含可变长度和逗号分隔的值字符串的熊猫行列如何堆叠成单独的值? - How is a pandas column of rows containing variable length and comma separated strings of values, stacked into separate values? 使用 pandas 从逗号分隔的列创建新变量 - Using pandas to create new variables from a comma separated column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM