读取CSV文件并使用pandas编写函数

问题描述 投票:0回答:1

我是pandas的新手,但是想要学习它,但我要使用这个库创建一个函数。我有一个带虚拟数据的csv文件source.csv(链接到文件:pastebin)。其中的关键栏目是:month, area, name, errors。每个月,从MY WORKSarea,下面的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中的其余元素一起完成。上面显示的预期输出仅适用于WORLDP&G

python pandas
1个回答
0
投票

https://gist.github.com/kyogesh/c7c2e3e16e9d9477cad3d6477bfc4bd4

请看看我贴在这里的要点。这将给你结果。从这里,您可以根据您的要求格式化数据。

© www.soinside.com 2019 - 2024. All rights reserved.