我是pandas
的新手,但是想要学习它,但我要使用这个库创建一个函数。我有一个带虚拟数据的csv文件source.csv
(链接到文件:pastebin)。其中的关键栏目是:month, area, name, errors
。每个月,从MY WORKS
的area
,下面的works
应该过滤(?)。对于每项工作,应计算errors
列中的问题。如果没有错误,则应考虑0。
import pandas as pd
source_df = pd.read_csv('source.csv') # Sorry guys, don't know how to proceed from here
works = ['WORLD', 'P&G', 'PART D', 'BRIGHTS', 'NOTIFICATION',
'OOP', 'ABCD', 'CHANNEL', 'KENNY DISPLAY', 'Migration']
months = ['January', 'March', 'April', 'May', 'June']
# Expected output:
data = {'WORLD': {'categories': months,
'series': [{
'name': 'Big Issue',
'data': [0, 0, 0, 0, 0] # Number of Big Issues in those months
}, {
'name': 'Small Issue',
'data': [1, 0, 0, 0, 0] # Number of Small Issues in those months
}, {
'name': 'Monitoring',
'data': [0, 2, 0, 0, 0] # Number of Monitorings in those months
}, {
'name': 'Improvement',
'data': [0, 0, 0, 1, 0] # Number of Improvements in those months
}]
},
'P&G': {'categories': months,
'series': [{
'name': 'Big Issue',
'data': [0, 0, 0, 0, 0]
}, {
'name': 'Small Issue',
'data': [0, 0, 0, 1, 0]
}, {
'name': 'Monitoring',
'data': [0, 2, 0, 0, 0]
}, {
'name': 'Improvement',
'data': [0, 0, 0, 1, 0]
}]
}
}
字典输出将与works
中的其余元素一起完成。上面显示的预期输出仅适用于WORLD
和P&G
。
https://gist.github.com/kyogesh/c7c2e3e16e9d9477cad3d6477bfc4bd4
请看看我贴在这里的要点。这将给你结果。从这里,您可以根据您的要求格式化数据。