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]}
添加回答
舉報