我正在构建一个函数,该函数接受公司描述列表,然后输出列表中最常见的三词短语。我已经能够让它输出这样构造的元组字典:
{('technology', 'company', 'provides'): 2,
('various', 'industries.', 'company'): 2,
('provides', 'software', 'solutions'): 2,
('life', 'health', 'insurance'): 2,...}
我想将其转换为表/数据框,将字符串连接成单个值,然后创建一个单独的列来存储该短语的实例数。
理想的输出是:
我尝试使用以下方法将元组组合成一个字符串,但它会减少出现的次数:
# function that converts tuple to string
def join_tuple_string(descriptions) -> str:
return ' '.join(descriptions)
# joining all the tuples
result = map(join_tuple_string, descriptions)
# converting and printing the result
print(list(result))
这是输出:
['technology company provides',
'provides software solutions',
'product suite includes', 'life health insurance',...]
如何连接这些值而不丢失出现次数?我希望能够将其导出到 CSV 以查看完整列表。
最佳答案
import pandas as pd
result = {('technology', 'company', 'provides'): 2,
('various', 'industries.', 'company'): 2,
('provides', 'software', 'solutions'): 2,
('life', 'health', 'insurance'): 2}
df = pd.DataFrame(result.items(), columns=['phrase', 'occurrence'])
df.phrase = df.phrase.str.join(' ')
print(df)
df.to_csv('phrases.csv', index=False)
df 输出:
phrase occurrence
0 technology company provides 2
1 various industries. company 2
2 provides software solutions 2
3 life health insurance 2
csv 文件:
phrase,occurrence
technology company provides,2
various industries. company,2
provides software solutions,2
life health insurance,2
关于python - 将包含字符串和整数值的元组中的值组合起来并存储为数据帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69866261/