我正在将 CSV 文件读入熊猫。问题是文件需要删除其他行上的行和计算值。我现在的想法是这样开始的
with open(down_path.name) as csv_file:
rdr = csv.DictReader(csv_file)
for row in rdr:
type = row['']
if type == 'Summary':
current_ward = row['Name']
else:
name = row['Name']
count1 = row['Count1']
count2 = row['Count2']
count3 = row['Count3']
index_count += 1
# write to someplace
,Name,count1,count2,count3
Ward Summary,Aloha 1,35,0,0
Individual Statistics,John,35,0,0
Ward Summary,Aloha I,794,0,0
Individual Statistics,Walter,476,0,0
Individual Statistics,Deborah,182,0,0
最终结果需要以我可以连接到现有数据帧的数据帧结束。
Braindead 的方法就是简单地进行转换并创建一个新的 CSV 文件,然后将其读入。似乎是一种非 Pythonic 方式。
需要取出摘要行,将具有相似名称的那些(Aloha 1 和 Aloha I)合并,删除个人统计信息并在每个人上贴上 Aloha 1 标签。另外我需要添加这些数据来自哪个月份。如您所见,数据需要一些工作:)
期望的输出是 Jan-16, Aloha 1, John, 1,2,3
Aloha 1 来自其上方的摘要行