4 回答
TA贡献1852条经验 获得超1个赞
这就是我最终使用的:
fhand = csv.reader(fopen)
next(fhand)
category_sum = dict()
word_sum = dict()
for row in fhand:
num_words = len(row[0].split(" ")) + len(row[1].split(" "))
if row[2] in category_sum.keys():
category_sum[row[2]]+=1
word_sum[row[2]]+=num_words
else:
category_sum[row[2]]=1
word_sum[row[2]]=num_words
combined = {key:[category_sum[key],word_sum[key]] for key in category_sum}
#print(combined)
print("Category | # Titles | # of Words\n---------------------------------")
for key in combined:
print("{} | {} | {} ".format(key,combined[key][0],combined[key][1]))
TA贡献1834条经验 获得超8个赞
你可以这样做:
import csv
file_name = 'book_titles.csv'
with open(file_name, 'r', newline='') as fopen:
reader = csv.reader(fopen)
next(reader) # Skip header.
category_sum = {}
for row in reader:
category_sum[row[2]] = category_sum.get(row[2], 0) + 1
print(category_sum) # -> {'Fiction': 1, 'Non-Fiction': 2}
TA贡献1815条经验 获得超6个赞
您可以将字典保存为其中一个键Count和另一个键所在的值Num-Words。因此,您的字典值分配可能如下所示:
# num_of_words =
if row[2] in category_sum.keys():
category_sum[row[2]]['Count']+=1
category_sum[row[2]]['Num-Words']+=num_of_words
else:
category_sum[row[2]]={}
TA贡献1810条经验 获得超4个赞
使用pandas
:
创建数据框
合并两个标题,按空格分割并计算由创建的列表中的单词
split
groupby
onType
,然后聚合count
和sum
函数。reset_index
并rename
获得所需的确切形式。
import pandas as pd
# read the file in
df = pd.read_csv('file.csv')
Title_1 Title_2 Type
He heard it from space A quick story about sounds from space Fiction
The end of all time A sad poem about the end of time Non-Fiction
The perfect beginning A story about friendship Non-Fiction
# count the words in Title_1 & Title_2
df['num_words'] = df[['Title_1', 'Title_2']].apply(lambda x: len(f'{x[0]} {x[1]}'.split()), axis=1)
Title_1 Title_2 Type num_words
He heard it from space A quick story about sounds from space Fiction 12
The end of all time A sad poem about the end of time Non-Fiction 13
The perfect beginning A story about friendship Non-Fiction 7
# create your desired output
test = df[['Type', 'num_words']].groupby('Type')['num_words'].agg(['count', 'sum']).reset_index().rename(columns={'count': 'Count', 'sum': 'Num-words'})
Type Count Num-words
Fiction 1 12
Non-Fiction 2 20
只需 3 行代码即可获得所需的输出
使用数据框中的数据,如果需要,您可以更轻松地执行其他类型的文本分析(例如文本分析:使用 python 查找列中最常见的单词)
在 a 中获取输出dict:
test.to_dict('list')
>>> {'Type': ['Fiction', 'Non-Fiction'], 'Count': [1, 2], 'Num-words': [12, 20]}
添加回答
举报