【问题标题】:Cleaning and merging csv files python清理和合并csv文件python
【发布时间】:2022-01-06 20:04:35
【问题描述】:

所以我已经在网上搜索了一段时间,试图找到清理和合并一组准备工作的 .csv 文件的最佳方法。我已经设法获得了一些可以满足我需求的代码,但它并不完整,它在运行时基本上什么都不做。这是我迄今为止的清洁工作:

    import pandas as pd
    import numpy as np

    #pick out empty spaces in the csv file
    missing_values = ["--", " ", "0","-"]
    dff = pd.read_csv("principal_offence_category_august_2014.csv", na_values = missing_values)

    print(dff['Percentage of Homicide Convictions'])
    print(dff['Percentage of Homicide Convictions'].isnull())

    #detecting numbers
    cnt=0
    for row in dff['Percentage of Homicide Convictions']:
        try:
            int(row)
            dff.loc[cnt, 'Percentage of Homicide Convictions'] = np.nan
        except ValueError:
            pass
        cnt = cnt + 1

    #replace empty values with 0
    dff['Percentage of Homicide Convictions'].fillna(0, inplace=True)

    #drop row with nan
    dff = dff.dropna()


    print(dff)

代码输出:

RESTART: C:\Users\Kinkerman\Downloads\Uni\Data Analytics\Dataset_Assignment\2014\Combine    mod.py

我的合并代码也有问题,它什么也没返回。而且我不确定如何标记它们以便在合并时区分它们。这是我的代码:

    import pandas as pd

    path = "C:/Users/Kinkerman/Downloads/Uni/Data     Analytics/Dataset_Assignment/2014"

    part1 = pd.read_csv("principal_offence_category_april_2014.csv")
    part2 = pd.read_csv("principal_offence_category_august_2014.csv")
    part3 = pd.read_csv("principal_offence_category_december_2014.csv")
    part4 = pd.read_csv("principal_offence_category_february_2014.csv")
    part5 = pd.read_csv("principal_offence_category_january_2014.csv")
    part6 = pd.read_csv("principal_offence_category_july_2014.csv")
    part7 = pd.read_csv("principal_offence_category_june_2014.csv")
    part8 = pd.read_csv("principal_offence_category_march_2014.csv")
    part9 = pd.read_csv("principal_offence_category_may_2014.csv")
    part10 = pd.read_csv("principal_offence_category_november_2014.csv")
    part11 = pd.read_csv("principal_offence_category_october_2014.csv")
    part12 = pd.read_csv("principal_offence_category_september_2014.csv")

    combined_csv = pd.concat([part1, part2, part3, part4, part5, part6, part7, part8, part9, part10, part11, part12])

    combined_csv.drop(["Unnamed: 0"], inplace=True, axis=1)

    combined_csv.to_csv("principal_offence_year_2014")

代码输出:

什么都没有

        Number of Homicide Convictions  Percentage of Homicide Convictions  Number of Homicide Unsuccessful Percentage of Homicide Unsuccessful Number of Offences Against The Person Convictions   Percentage of Offences Against The Person Convictions   Number of Offences Against The Person Unsuccessful  Percentage of Offences Against The Person Unsuccessful  Number of Sexual Offences Convictions   Percentage of Sexual Offences Convictions   Number of Sexual Offences Unsuccessful  Percentage of Sexual Offences Unsuccessful  Number of Burglary Convictions  Percentage of Burglary Convictions  Number of Burglary Unsuccessful Percentage of Burglary Unsuccessful Number of Robbery Convictions   Percentage of Robbery Convictions   Number of Robbery Unsuccessful  Percentage of Robbery Unsuccessful  Number of Theft And Handling Convictions    Percentage of Theft And Handling Convictions    Number of Theft And Handling Unsuccessful   Percentage of Theft And Handling Unsuccessful   Number of Fraud And Forgery Convictions Percentage of Fraud And Forgery Convictions Number of Fraud And Forgery Unsuccessful    Percentage of Fraud And Forgery Unsuccessful    Number of Criminal Damage Convictions   Percentage of Criminal Damage Convictions   Number of Criminal Damage Unsuccessful  Percentage of Criminal Damage Unsuccessful  Number of Drugs Offences Convictions    Percentage of Drugs Offences Convictions    Number of Drugs Offences Unsuccessful   Percentage of Drugs Offences Unsuccessful   Number of Public Order Offences Convictions Percentage of Public Order Offences Convictions Number of Public Order Offences Unsuccessful    Percentage of Public Order Offences Unsuccessful    Number of All Other Offences (excluding Motoring) Convictions   Percentage of All Other Offences (excluding Motoring) Convictions   Number of All Other Offences (excluding Motoring) Unsuccessful  Percentage of All Other Offences (excluding Motoring) Unsuccessful  Number of Motoring Offences Convictions Percentage of Motoring Offences Convictions Number of Motoring Offences Unsuccessful    Percentage of Motoring Offences Unsuccessful    Number of Admin Finalised Unsuccessful  Percentage of L Motoring Offences Unsuccessful
National    81  85.30%  14  14.70%  7,805   74.10%  2,722   25.90%  698 72.20%  269 27.80%  1,470   86.70%  226 13.30%  517 81.70%  116 18.30%  10,045  92.30%  840 7.70%   666 86.00%  108 14.00%  2,259   85.20%  391 14.80%  4,536   94.20%  279 5.80%   3,549   84.40%  654 15.60%  2,640   83.70%  513 16.30%  8,283   86.30%  1,314   13.70%  718 100.00%
Avon and Somerset   1   100.00% 0   0.00%   167 78.80%  45  21.20%  36  81.80%  8   18.20%  37  94.90%  2   5.10%   9   75.00%  3   25.00%  266 92.70%  21  7.30%   11  100.00% 0   0.00%   54  90.00%  6   10.00%  135 98.50%  2   1.50%   68  86.10%  11  13.90%  66  80.50%  16  19.50%  188 83.60%  37  16.40%  24  100.00%
Bedfordshire    0   -   0   -   69  75.00%  23  25.00%  5   83.30%  1   16.70%  16  94.10%  1   5.90%   4   100.00% 0   0.00%   98  91.60%  9   8.40%   8   80.00%  2   20.00%  20  76.90%  6   23.10%  45  95.70%  2   4.30%   29  82.90%  6   17.10%  11  64.70%  6   35.30%  40  88.90%  5   11.10%  16  100.00%
Cambridgeshire  0   -   0   -   99  81.10%  23  18.90%  6   66.70%  3   33.30%  8   100.00% 0   0.00%   6   85.70%  1   14.30%  107 91.50%  10  8.50%   7   100.00% 0   0.00%   21  95.50%  1   4.50%   40  95.20%  2   4.80%   45  83.30%  9   16.70%  6   75.00%  2   25.00%  79  92.90%  6   7.10%   4   100.00%
Cheshire    1   50.00%  1   50.00%  140 74.90%  47  25.10%  17  85.00%  3   15.00%  26  89.70%  3   10.30%  1   100.00% 0   0.00%   206 98.10%  4   1.90%   16  88.90%  2   11.10%  35  79.50%  9   20.50%  75  88.20%  10  11.80%  86  92.50%  7   7.50%   50  89.30%  6   10.70%  209 94.60%  12  5.40%   1   100.00%
Cleveland   0   -   0   -   85  67.50%  41  32.50%  11  73.30%  4   26.70%  25  71.40%  10  28.60%  5   71.40%  2   28.60%  254 88.80%  32  11.20%  6   75.00%  2   25.00%  32  80.00%  8   20.00%  63  90.00%  7   10.00%  74  73.30%  27  26.70%  28  84.80%  5   15.20%  124 87.90%  17  12.10%  10  100.00%
Cumbria 0   -   0   -   77  80.20%  19  19.80%  8   88.90%  1   11.10%  12  92.30%  1   7.70%   1   100.00% 0   0.00%   108 94.70%  6   5.30%   5   100.00% 0   0.00%   37  94.90%  2   5.10%   42  95.50%  2   4.50%   40  95.20%  2   4.80%   64  98.50%  1   1.50%   95  90.50%  10  9.50%   12  100.00%
Derbyshire  0   -   0   -   151 72.60%  57  27.40%  8   57.10%  6   42.90%  31  91.20%  3   8.80%   8   72.70%  3   27.30%  203 93.10%  15  6.90%   11  55.00%  9   45.00%  40  85.10%  7   14.90%  75  89.30%  9   10.70%  50  92.60%  4   7.40%   46  75.40%  15  24.60%  258 95.20%  13  4.80%   16  100.00%
Devon and Cornwall  1   100.00% 0   0.00%   157 75.80%  50  24.20%  11  73.30%  4   26.70%  16  94.10%  1   5.90%   6   100.00% 0   0.00%   151 93.80%  10  6.20%   8   100.00% 0   0.00%   56  90.30%  6   9.70%   70  92.10%  6   7.90%   65  76.50%  20  23.50%  64  82.10%  14  17.90%  189 91.70%  17  8.30%   15  100.00%
Dorset  0   -   0   -   73  82.00%  16  18.00%  1   100.00% 0   0.00%   18  94.70%  1   5.30%   3   100.00% 0   0.00%   123 91.80%  11  8.20%   7   77.80%  2   22.20%  24  85.70%  4   14.30%  29  93.50%  2   6.50%   45  93.80%  3   6.30%   25  96.20%  1   3.80%   71  91.00%  7   9.00%   5   100.00%
Durham  2   100.00% 0   0.00%   75  74.30%  26  25.70%  11  73.30%  4   26.70%  30  100.00% 0   0.00%   0   0.00%   1   100.00% 144 88.30%  19  11.70%  4   44.40%  5   55.60%  43  86.00%  7   14.00%  19  90.50%  2   9.50%   58  81.70%  13  18.30%  12  80.00%  3   20.00%  66  95.70%  3   4.30%   0   -
Dyfed Powys 0   -   0   -   63  70.80%  26  29.20%  0   0.00%   1   100.00% 17  85.00%  3   15.00%  2   100.00% 0   0.00%   56  91.80%  5   8.20%   2   66.70%  1   33.30%  27  87.10%  4   12.90%  76  93.80%  5   6.20%   28  71.80%  11  28.20%  20  100.00% 0   0.00%   178 92.20%  15  7.80%   5   100.00%
Essex   1   100.00% 0   0.00%   261 79.30%  68  20.70%  20  74.10%  7   25.90%  39  76.50%  12  23.50%  6   75.00%  2   25.00%  280 96.60%  10  3.40%   16  80.00%  4   20.00%  63  80.80%  15  19.20%  97  99.00%  1   1.00%   79  96.30%  3   3.70%   52  82.50%  11  17.50%  175 89.30%  21  10.70%  20  100.00%
Gloucestershire 0   -   0   -   41  75.90%  13  24.10%  4   80.00%  1   20.00%  13  100.00% 0   0.00%   0   0.00%   1   100.00% 68  89.50%  8   10.50%  9   81.80%  2   18.20%  14  93.30%  1   6.70%   22  88.00%  3   12.00%  14  87.50%  2   12.50%  19  76.00%  6   24.00%  80  94.10%  5   5.90%   11  100.00%
GreaterManchester   1   100.00% 0   0.00%   471 82.10%  103 17.90%  44  73.30%  16  26.70%  87  85.30%  15  14.70%  29  78.40%  8   21.60%  532 96.00%  22  4.00%   51  87.90%  7   12.10%  172 90.10%  19  9.90%   239 94.10%  15  5.90%   249 92.60%  20  7.40%   132 91.00%  13  9.00%   455 87.00%  68  13.00%  47  100.00%
Gwent   0   -   0   -   77  74.80%  26  25.20%  6   50.00%  6   50.00%  20  80.00%  5   20.00%  2   100.00% 0   0.00%   124 94.70%  7   5.30%   8   100.00% 0   0.00%   20  71.40%  8   28.60%  55  94.80%  3   5.20%   58  81.70%  13  18.30%  16  80.00%  4   20.00%  66  76.70%  20  23.30%  4   100.00%
Hampshire   2   100.00% 0   0.00%   253 74.20%  88  25.80%  18  64.30%  10  35.70%  38  82.60%  8   17.40%  8   61.50%  5   38.50%  317 92.40%  26  7.60%   19  76.00%  6   24.00%  79  86.80%  12  13.20%  134 91.20%  13  8.80%   95  84.10%  18  15.90%  104 83.20%  21  16.80%  227 90.40%  24  9.60%   22  100.00%
Hertfordshire   1   100.00% 0   0.00%   132 77.20%  39  22.80%  15  83.30%  3   16.70%  29  87.90%  4   12.10%  12  100.00% 0   0.00%   210 89.40%  25  10.60%  23  82.10%  5   17.90%  47  94.00%  3   6.00%   114 93.40%  8   6.60%   83  91.20%  8   8.80%   12  85.70%  2   14.30%  158 86.80%  24  13.20%  22  100.00%
Humberside  0   -   0   -   181 88.30%  24  11.70%  12  60.00%  8   40.00%  41  85.40%  7   14.60%  5   100.00% 0   0.00%   229 94.20%  14  5.80%   14  93.30%  1   6.70%   44  93.60%  3   6.40%   55  94.80%  3   5.20%   96  88.90%  12  11.10%  22  81.50%  5   18.50%  119 83.20%  24  16.80%  13  100.00%
Kent    9   64.30%  5   35.70%  235 76.10%  74  23.90%  17  60.70%  11  39.30%  44  97.80%  1   2.20%   9   75.00%  3   25.00%  305 90.20%  33  9.80%   11  78.60%  3   21.40%  65  74.70%  22  25.30%  109 96.50%  4   3.50%   36  72.00%  14  28.00%  125 81.70%  28  18.30%  164 91.10%  16  8.90%   17  100.00%
Lancashire  1   100.00% 0   0.00%   265 70.30%  112 29.70%  30  69.80%  13  30.20%  63  85.10%  11  14.90%  9   81.80%  2   18.20%  272 94.80%  15  5.20%   19  82.60%  4   17.40%  93  83.80%  18  16.20%  114 92.70%  9   7.30%   147 90.70%  15  9.30%   113 89.70%  13  10.30%  256 84.50%  47  15.50%  17  100.00%
Leicestershire  4   80.00%  1   20.00%  140 78.70%  38  21.30%  6   54.50%  5   45.50%  26  81.30%  6   18.80%  4   50.00%  4   50.00%  166 94.30%  10  5.70%   10  90.90%  1   9.10%   38  84.40%  7   15.60%  36  97.30%  1   2.70%   53  85.50%  9   14.50%  17  77.30%  5   22.70%  193 92.80%  15  7.20%   4   100.00%
Lincolnshire    0   -   0   -   89  83.20%  18  16.80%  10  90.90%  1   9.10%   20  90.90%  2   9.10%   2   100.00% 0   0.00%   138 92.60%  11  7.40%   5   83.30%  1   16.70%  17  100.00% 0   0.00%   26  92.90%  2   7.10%   44  84.60%  8   15.40%  23  82.10%  5   17.90%  125 89.90%  14  10.10%  11  100.00%
Merseyside  5   100.00% 0   0.00%   228 79.20%  60  20.80%  17  89.50%  2   10.50%  33  82.50%  7   17.50%  8   61.50%  5   38.50%  298 92.30%  25  7.70%   12  92.30%  1   7.70%   56  86.20%  9   13.80%  285 96.90%  9   3.10%   101 75.90%  32  24.10%  106 88.30%  14  11.70%  222 89.50%  26  10.50%  21  100.00%
Metropolitan and City   21  91.30%  2   8.70%   1,129   67.40%  546 32.60%  117 65.70%  61  34.30%  224 84.20%  42  15.80%  179 85.60%  30  14.40%  1,273   91.00%  126 9.00%   179 87.70%  25  12.30%  271 82.90%  56  17.10%  1,098   93.90%  71  6.10%   639 81.50%  145 18.50%  482 86.40%  76  13.60%  1,404   77.60%  406 22.40%  180 100.00%
Norfolk 1   100.00% 0   0.00%   165 79.30%  43  20.70%  8   80.00%  2   20.00%  12  85.70%  2   14.30%  3   100.00% 0   0.00%   146 91.30%  14  8.80%   11  91.70%  1   8.30%   52  91.20%  5   8.80%   76  96.20%  3   3.80%   58  86.60%  9   13.40%  49  90.70%  5   9.30%   107 87.70%  15  12.30%  9   100.00%
Northamptonshire    2   100.00% 0   0.00%   69  65.70%  36  34.30%  13  68.40%  6   31.60%  23  92.00%  2   8.00%   11  91.70%  1   8.30%   135 86.00%  22  14.00%  7   87.50%  1   12.50%  21  91.30%  2   8.70%   31  93.90%  2   6.10%   44  83.00%  9   17.00%  11  78.60%  3   21.40%  97  89.00%  12  11.00%  8   100.00%
Northumbria 3   100.00% 0   0.00%   225 68.40%  104 31.60%  10  47.60%  11  52.40%  54  87.10%  8   12.90%  15  88.20%  2   11.80%  415 84.90%  74  15.10%  15  88.20%  2   11.80%  99  81.10%  23  18.90%  98  83.80%  19  16.20%  109 76.20%  34  23.80%  188 77.00%  56  23.00%  249 86.80%  38  13.20%  11  100.00%
North Wales 1   100.00% 0   0.00%   100 76.30%  31  23.70%  7   53.80%  6   46.20%  21  95.50%  1   4.50%   3   100.00% 0   0.00%   137 93.20%  10  6.80%   9   90.00%  1   10.00%  45  86.50%  7   13.50%  40  95.20%  2   4.80%   43  95.60%  2   4.40%   68  87.20%  10  12.80%  150 88.80%  19  11.20%  2   100.00%
North Yorkshire 1   50.00%  1   50.00%  92  86.80%  14  13.20%  5   100.00% 0   0.00%   6   75.00%  2   25.00%  6   85.70%  1   14.30%  137 90.70%  14  9.30%   6   85.70%  1   14.30%  19  67.90%  9   32.10%  38  95.00%  2   5.00%   42  87.50%  6   12.50%  11  55.00%  9   45.00%  78  90.70%  8   9.30%   14  100.00%
Nottinghamshire 2   100.00% 0   0.00%   182 71.90%  71  28.10%  12  80.00%  3   20.00%  30  100.00% 0   0.00%   11  57.90%  8   42.10%  241 96.00%  10  4.00%   10  100.00% 0   0.00%   45  86.50%  7   13.50%  100 96.20%  4   3.80%   46  82.10%  10  17.90%  51  78.50%  14  21.50%  154 92.80%  12  7.20%   2   100.00%
South Wales 0   -   0   -   232 72.50%  88  27.50%  13  65.00%  7   35.00%  55  94.80%  3   5.20%   10  100.00% 0   0.00%   289 94.40%  17  5.60%   9   90.00%  1   10.00%  64  97.00%  2   3.00%   112 100.00% 0   0.00%   150 93.20%  11  6.80%   47  87.00%  7   13.00%  323 81.20%  75  18.80%  11  100.00%
South Yorkshire 2   100.00% 0   0.00%   218 80.40%  53  19.60%  18  90.00%  2   10.00%  44  95.70%  2   4.30%   9   64.30%  5   35.70%  350 95.90%  15  4.10%   9   81.80%  2   18.20%  75  91.50%  7   8.50%   114 95.00%  6   5.00%   78  84.80%  14  15.20%  41  85.40%  7   14.60%  162 89.50%  19  10.50%  13  100.00%
Staffordshire   0   -   0   -   150 69.40%  66  30.60%  15  88.20%  2   11.80%  20  95.20%  1   4.80%   8   80.00%  2   20.00%  161 85.60%  27  14.40%  11  91.70%  1   8.30%   55  74.30%  19  25.70%  80  93.00%  6   7.00%   85  81.70%  19  18.30%  63  90.00%  7   10.00%  147 83.10%  30  16.90%  8   100.00%
Suffolk 1   100.00% 0   0.00%   138 87.30%  20  12.70%  7   100.00% 0   0.00%   14  73.70%  5   26.30%  4   80.00%  1   20.00%  111 95.70%  5   4.30%   5   83.30%  1   16.70%  28  90.30%  3   9.70%   40  97.60%  1   2.40%   61  91.00%  6   9.00%   43  86.00%  7   14.00%  68  88.30%  9   11.70%  5   100.00%
Surrey  0   -   0   -   93  75.60%  30  24.40%  11  61.10%  7   38.90%  5   62.50%  3   37.50%  8   88.90%  1   11.10%  111 88.80%  14  11.20%  17  94.40%  1   5.60%   23  82.10%  5   17.90%  55  96.50%  2   3.50%   39  83.00%  8   17.00%  37  71.20%  15  28.80%  134 87.60%  19  12.40%  17  100.00%
Sussex  0   -   0   -   171 80.30%  42  19.70%  15  65.20%  8   34.80%  35  87.50%  5   12.50%  7   100.00% 0   0.00%   190 91.80%  17  8.20%   14  73.70%  5   26.30%  36  85.70%  6   14.30%  80  95.20%  4   4.80%   72  84.70%  13  15.30%  74  72.50%  28  27.50%  161 89.40%  19  10.60%  17  100.00%
Thames Valley   9   100.00% 0   0.00%   214 65.20%  114 34.80%  26  72.20%  10  27.80%  40  87.00%  6   13.00%  10  62.50%  6   37.50%  296 92.50%  24  7.50%   15  83.30%  3   16.70%  51  82.30%  11  17.70%  122 93.10%  9   6.90%   105 84.00%  20  16.00%  32  76.20%  10  23.80%  280 89.70%  32  10.30%  40  100.00%
Warwickshire    0   -   0   -   35  76.10%  11  23.90%  7   87.50%  1   12.50%  10  100.00% 0   0.00%   1   100.00% 0   0.00%   76  96.20%  3   3.80%   1   100.00% 0   0.00%   10  83.30%  2   16.70%  18  94.70%  1   5.30%   15  78.90%  4   21.10%  32  91.40%  3   8.60%   154 90.60%  16  9.40%   0   -
West Mercia 1   50.00%  1   50.00%  156 78.80%  42  21.20%  24  85.70%  4   14.30%  14  82.40%  3   17.60%  5   83.30%  1   16.70%  169 93.40%  12  6.60%   5   100.00% 0   0.00%   47  92.20%  4   7.80%   55  98.20%  1   1.80%   66  86.80%  10  13.20%  49  89.10%  6   10.90%  198 88.80%  25  11.20%  3   100.00%
West Midlands   6   66.70%  3   33.30%  413 64.90%  223 35.10%  28  75.70%  9   24.30%  90  82.60%  19  17.40%  55  77.50%  16  22.50%  558 92.20%  47  7.80%   35  97.20%  1   2.80%   109 83.20%  22  16.80%  226 92.60%  18  7.40%   116 77.90%  33  22.10%  164 79.60%  42  20.40%  304 86.10%  49  13.90%  16  100.00%
West Yorkshire  1   100.00% 0   0.00%   420 76.60%  128 23.40%  40  83.30%  8   16.70%  78  80.40%  19  19.60%  30  93.80%  2   6.30%   505 93.70%  34  6.30%   22  88.00%  3   12.00%  86  81.90%  19  18.10%  173 95.60%  8   4.40%   116 80.00%  29  20.00%  52  83.90%  10  16.10%  297 85.10%  52  14.90%  35  100.00%
Wiltshire   1   100.00% 0   0.00%   74  75.50%  24  24.50%  9   69.20%  4   30.80%  6   100.00% 0   0.00%   3   100.00% 0   0.00%   90  92.80%  7   7.20%   4   80.00%  1   20.00%  26  83.90%  5   16.10%  25  100.00% 0   0.00%   22  81.50%  5   18.50%  13  86.70%  2   13.30%  79  85.90%  13  14.10%  10  100.00%
    Number of Homicide Convictions  Percentage of Homicide Convictions  Number of Homicide Unsuccessful Percentage of Homicide Unsuccessful Number of Offences Against The Person Convictions   Percentage of Offences Against The Person Convictions   Number of Offences Against The Person Unsuccessful  Percentage of Offences Against The Person Unsuccessful  Number of Sexual Offences Convictions   Percentage of Sexual Offences Convictions   Number of Sexual Offences Unsuccessful  Percentage of Sexual Offences Unsuccessful  Number of Burglary Convictions  Percentage of Burglary Convictions  Number of Burglary Unsuccessful Percentage of Burglary Unsuccessful Number of Robbery Convictions   Percentage of Robbery Convictions   Number of Robbery Unsuccessful  Percentage of Robbery Unsuccessful  Number of Theft And Handling Convictions    Percentage of Theft And Handling Convictions    Number of Theft And Handling Unsuccessful   Percentage of Theft And Handling Unsuccessful   Number of Fraud And Forgery Convictions Percentage of Fraud And Forgery Convictions Number of Fraud And Forgery Unsuccessful    Percentage of Fraud And Forgery Unsuccessful    Number of Criminal Damage Convictions   Percentage of Criminal Damage Convictions   Number of Criminal Damage Unsuccessful  Percentage of Criminal Damage Unsuccessful  Number of Drugs Offences Convictions    Percentage of Drugs Offences Convictions    Number of Drugs Offences Unsuccessful   Percentage of Drugs Offences Unsuccessful   Number of Public Order Offences Convictions Percentage of Public Order Offences Convictions Number of Public Order Offences Unsuccessful    Percentage of Public Order Offences Unsuccessful    Number of All Other Offences (excluding Motoring) Convictions   Percentage of All Other Offences (excluding Motoring) Convictions   Number of All Other Offences (excluding Motoring) Unsuccessful  Percentage of All Other Offences (excluding Motoring) Unsuccessful  Number of Motoring Offences Convictions Percentage of Motoring Offences Convictions Number of Motoring Offences Unsuccessful    Percentage of Motoring Offences Unsuccessful    Number of Admin Finalised Unsuccessful  Percentage of L Motoring Offences Unsuccessful
National    81  85.30%  14  14.70%  7,805   74.10%  2,722   25.90%  698 72.20%  269 27.80%  1,470   86.70%  226 13.30%  517 81.70%  116 18.30%  10,045  92.30%  840 7.70%   666 86.00%  108 14.00%  2,259   85.20%  391 14.80%  4,536   94.20%  279 5.80%   3,549   84.40%  654 15.60%  2,640   83.70%  513 16.30%  8,283   86.30%  1,314   13.70%  718 100.00%
Avon and Somerset   1   100.00% 0   0.00%   167 78.80%  45  21.20%  36  81.80%  8   18.20%  37  94.90%  2   5.10%   9   75.00%  3   25.00%  266 92.70%  21  7.30%   11  100.00% 0   0.00%   54  90.00%  6   10.00%  135 98.50%  2   1.50%   68  86.10%  11  13.90%  66  80.50%  16  19.50%  188 83.60%  37  16.40%  24  100.00%
Bedfordshire    0   -   0   -   69  75.00%  23  25.00%  5   83.30%  1   16.70%  16  94.10%  1   5.90%   4   100.00% 0   0.00%   98  91.60%  9   8.40%   8   80.00%  2   20.00%  20  76.90%  6   23.10%  45  95.70%  2   4.30%   29  82.90%  6   17.10%  11  64.70%  6   35.30%  40  88.90%  5   11.10%  16  100.00%
Cambridgeshire  0   -   0   -   99  81.10%  23  18.90%  6   66.70%  3   33.30%  8   100.00% 0   0.00%   6   85.70%  1   14.30%  107 91.50%  10  8.50%   7   100.00% 0   0.00%   21  95.50%  1   4.50%   40  95.20%  2   4.80%   45  83.30%  9   16.70%  6   75.00%  2   25.00%  79  92.90%  6   7.10%   4   100.00%
Cheshire    1   50.00%  1   50.00%  140 74.90%  47  25.10%  17  85.00%  3   15.00%  26  89.70%  3   10.30%  1   100.00% 0   0.00%   206 98.10%  4   1.90%   16  88.90%  2   11.10%  35  79.50%  9   20.50%  75  88.20%  10  11.80%  86  92.50%  7   7.50%   50  89.30%  6   10.70%  209 94.60%  12  5.40%   1   100.00%
Cleveland   0   -   0   -   85  67.50%  41  32.50%  11  73.30%  4   26.70%  25  71.40%  10  28.60%  5   71.40%  2   28.60%  254 88.80%  32  11.20%  6   75.00%  2   25.00%  32  80.00%  8   20.00%  63  90.00%  7   10.00%  74  73.30%  27  26.70%  28  84.80%  5   15.20%  124 87.90%  17  12.10%  10  100.00%
Cumbria 0   -   0   -   77  80.20%  19  19.80%  8   88.90%  1   11.10%  12  92.30%  1   7.70%   1   100.00% 0   0.00%   108 94.70%  6   5.30%   5   100.00% 0   0.00%   37  94.90%  2   5.10%   42  95.50%  2   4.50%   40  95.20%  2   4.80%   64  98.50%  1   1.50%   95  90.50%  10  9.50%   12  100.00%
Derbyshire  0   -   0   -   151 72.60%  57  27.40%  8   57.10%  6   42.90%  31  91.20%  3   8.80%   8   72.70%  3   27.30%  203 93.10%  15  6.90%   11  55.00%  9   45.00%  40  85.10%  7   14.90%  75  89.30%  9   10.70%  50  92.60%  4   7.40%   46  75.40%  15  24.60%  258 95.20%  13  4.80%   16  100.00%
Devon and Cornwall  1   100.00% 0   0.00%   157 75.80%  50  24.20%  11  73.30%  4   26.70%  16  94.10%  1   5.90%   6   100.00% 0   0.00%   151 93.80%  10  6.20%   8   100.00% 0   0.00%   56  90.30%  6   9.70%   70  92.10%  6   7.90%   65  76.50%  20  23.50%  64  82.10%  14  17.90%  189 91.70%  17  8.30%   15  100.00%
Dorset  0   -   0   -   73  82.00%  16  18.00%  1   100.00% 0   0.00%   18  94.70%  1   5.30%   3   100.00% 0   0.00%   123 91.80%  11  8.20%   7   77.80%  2   22.20%  24  85.70%  4   14.30%  29  93.50%  2   6.50%   45  93.80%  3   6.30%   25  96.20%  1   3.80%   71  91.00%  7   9.00%   5   100.00%
Durham  2   100.00% 0   0.00%   75  74.30%  26  25.70%  11  73.30%  4   26.70%  30  100.00% 0   0.00%   0   0.00%   1   100.00% 144 88.30%  19  11.70%  4   44.40%  5   55.60%  43  86.00%  7   14.00%  19  90.50%  2   9.50%   58  81.70%  13  18.30%  12  80.00%  3   20.00%  66  95.70%  3   4.30%   0   -
Dyfed Powys 0   -   0   -   63  70.80%  26  29.20%  0   0.00%   1   100.00% 17  85.00%  3   15.00%  2   100.00% 0   0.00%   56  91.80%  5   8.20%   2   66.70%  1   33.30%  27  87.10%  4   12.90%  76  93.80%  5   6.20%   28  71.80%  11  28.20%  20  100.00% 0   0.00%   178 92.20%  15  7.80%   5   100.00%
Essex   1   100.00% 0   0.00%   261 79.30%  68  20.70%  20  74.10%  7   25.90%  39  76.50%  12  23.50%  6   75.00%  2   25.00%  280 96.60%  10  3.40%   16  80.00%  4   20.00%  63  80.80%  15  19.20%  97  99.00%  1   1.00%   79  96.30%  3   3.70%   52  82.50%  11  17.50%  175 89.30%  21  10.70%  20  100.00%
Gloucestershire 0   -   0   -   41  75.90%  13  24.10%  4   80.00%  1   20.00%  13  100.00% 0   0.00%   0   0.00%   1   100.00% 68  89.50%  8   10.50%  9   81.80%  2   18.20%  14  93.30%  1   6.70%   22  88.00%  3   12.00%  14  87.50%  2   12.50%  19  76.00%  6   24.00%  80  94.10%  5   5.90%   11  100.00%
GreaterManchester   1   100.00% 0   0.00%   471 82.10%  103 17.90%  44  73.30%  16  26.70%  87  85.30%  15  14.70%  29  78.40%  8   21.60%  532 96.00%  22  4.00%   51  87.90%  7   12.10%  172 90.10%  19  9.90%   239 94.10%  15  5.90%   249 92.60%  20  7.40%   132 91.00%  13  9.00%   455 87.00%  68  13.00%  47  100.00%
Gwent   0   -   0   -   77  74.80%  26  25.20%  6   50.00%  6   50.00%  20  80.00%  5   20.00%  2   100.00% 0   0.00%   124 94.70%  7   5.30%   8   100.00% 0   0.00%   20  71.40%  8   28.60%  55  94.80%  3   5.20%   58  81.70%  13  18.30%  16  80.00%  4   20.00%  66  76.70%  20  23.30%  4   100.00%
Hampshire   2   100.00% 0   0.00%   253 74.20%  88  25.80%  18  64.30%  10  35.70%  38  82.60%  8   17.40%  8   61.50%  5   38.50%  317 92.40%  26  7.60%   19  76.00%  6   24.00%  79  86.80%  12  13.20%  134 91.20%  13  8.80%   95  84.10%  18  15.90%  104 83.20%  21  16.80%  227 90.40%  24  9.60%   22  100.00%
Hertfordshire   1   100.00% 0   0.00%   132 77.20%  39  22.80%  15  83.30%  3   16.70%  29  87.90%  4   12.10%  12  100.00% 0   0.00%   210 89.40%  25  10.60%  23  82.10%  5   17.90%  47  94.00%  3   6.00%   114 93.40%  8   6.60%   83  91.20%  8   8.80%   12  85.70%  2   14.30%  158 86.80%  24  13.20%  22  100.00%
Humberside  0   -   0   -   181 88.30%  24  11.70%  12  60.00%  8   40.00%  41  85.40%  7   14.60%  5   100.00% 0   0.00%   229 94.20%  14  5.80%   14  93.30%  1   6.70%   44  93.60%  3   6.40%   55  94.80%  3   5.20%   96  88.90%  12  11.10%  22  81.50%  5   18.50%  119 83.20%  24  16.80%  13  100.00%
Kent    9   64.30%  5   35.70%  235 76.10%  74  23.90%  17  60.70%  11  39.30%  44  97.80%  1   2.20%   9   75.00%  3   25.00%  305 90.20%  33  9.80%   11  78.60%  3   21.40%  65  74.70%  22  25.30%  109 96.50%  4   3.50%   36  72.00%  14  28.00%  125 81.70%  28  18.30%  164 91.10%  16  8.90%   17  100.00%
Lancashire  1   100.00% 0   0.00%   265 70.30%  112 29.70%  30  69.80%  13  30.20%  63  85.10%  11  14.90%  9   81.80%  2   18.20%  272 94.80%  15  5.20%   19  82.60%  4   17.40%  93  83.80%  18  16.20%  114 92.70%  9   7.30%   147 90.70%  15  9.30%   113 89.70%  13  10.30%  256 84.50%  47  15.50%  17  100.00%
Leicestershire  4   80.00%  1   20.00%  140 78.70%  38  21.30%  6   54.50%  5   45.50%  26  81.30%  6   18.80%  4   50.00%  4   50.00%  166 94.30%  10  5.70%   10  90.90%  1   9.10%   38  84.40%  7   15.60%  36  97.30%  1   2.70%   53  85.50%  9   14.50%  17  77.30%  5   22.70%  193 92.80%  15  7.20%   4   100.00%
Lincolnshire    0   -   0   -   89  83.20%  18  16.80%  10  90.90%  1   9.10%   20  90.90%  2   9.10%   2   100.00% 0   0.00%   138 92.60%  11  7.40%   5   83.30%  1   16.70%  17  100.00% 0   0.00%   26  92.90%  2   7.10%   44  84.60%  8   15.40%  23  82.10%  5   17.90%  125 89.90%  14  10.10%  11  100.00%
Merseyside  5   100.00% 0   0.00%   228 79.20%  60  20.80%  17  89.50%  2   10.50%  33  82.50%  7   17.50%  8   61.50%  5   38.50%  298 92.30%  25  7.70%   12  92.30%  1   7.70%   56  86.20%  9   13.80%  285 96.90%  9   3.10%   101 75.90%  32  24.10%  106 88.30%  14  11.70%  222 89.50%  26  10.50%  21  100.00%
Metropolitan and City   21  91.30%  2   8.70%   1,129   67.40%  546 32.60%  117 65.70%  61  34.30%  224 84.20%  42  15.80%  179 85.60%  30  14.40%  1,273   91.00%  126 9.00%   179 87.70%  25  12.30%  271 82.90%  56  17.10%  1,098   93.90%  71  6.10%   639 81.50%  145 18.50%  482 86.40%  76  13.60%  1,404   77.60%  406 22.40%  180 100.00%
Norfolk 1   100.00% 0   0.00%   165 79.30%  43  20.70%  8   80.00%  2   20.00%  12  85.70%  2   14.30%  3   100.00% 0   0.00%   146 91.30%  14  8.80%   11  91.70%  1   8.30%   52  91.20%  5   8.80%   76  96.20%  3   3.80%   58  86.60%  9   13.40%  49  90.70%  5   9.30%   107 87.70%  15  12.30%  9   100.00%
Northamptonshire    2   100.00% 0   0.00%   69  65.70%  36  34.30%  13  68.40%  6   31.60%  23  92.00%  2   8.00%   11  91.70%  1   8.30%   135 86.00%  22  14.00%  7   87.50%  1   12.50%  21  91.30%  2   8.70%   31  93.90%  2   6.10%   44  83.00%  9   17.00%  11  78.60%  3   21.40%  97  89.00%  12  11.00%  8   100.00%
Northumbria 3   100.00% 0   0.00%   225 68.40%  104 31.60%  10  47.60%  11  52.40%  54  87.10%  8   12.90%  15  88.20%  2   11.80%  415 84.90%  74  15.10%  15  88.20%  2   11.80%  99  81.10%  23  18.90%  98  83.80%  19  16.20%  109 76.20%  34  23.80%  188 77.00%  56  23.00%  249 86.80%  38  13.20%  11  100.00%
North Wales 1   100.00% 0   0.00%   100 76.30%  31  23.70%  7   53.80%  6   46.20%  21  95.50%  1   4.50%   3   100.00% 0   0.00%   137 93.20%  10  6.80%   9   90.00%  1   10.00%  45  86.50%  7   13.50%  40  95.20%  2   4.80%   43  95.60%  2   4.40%   68  87.20%  10  12.80%  150 88.80%  19  11.20%  2   100.00%
North Yorkshire 1   50.00%  1   50.00%  92  86.80%  14  13.20%  5   100.00% 0   0.00%   6   75.00%  2   25.00%  6   85.70%  1   14.30%  137 90.70%  14  9.30%   6   85.70%  1   14.30%  19  67.90%  9   32.10%  38  95.00%  2   5.00%   42  87.50%  6   12.50%  11  55.00%  9   45.00%  78  90.70%  8   9.30%   14  100.00%
Nottinghamshire 2   100.00% 0   0.00%   182 71.90%  71  28.10%  12  80.00%  3   20.00%  30  100.00% 0   0.00%   11  57.90%  8   42.10%  241 96.00%  10  4.00%   10  100.00% 0   0.00%   45  86.50%  7   13.50%  100 96.20%  4   3.80%   46  82.10%  10  17.90%  51  78.50%  14  21.50%  154 92.80%  12  7.20%   2   100.00%
South Wales 0   -   0   -   232 72.50%  88  27.50%  13  65.00%  7   35.00%  55  94.80%  3   5.20%   10  100.00% 0   0.00%   289 94.40%  17  5.60%   9   90.00%  1   10.00%  64  97.00%  2   3.00%   112 100.00% 0   0.00%   150 93.20%  11  6.80%   47  87.00%  7   13.00%  323 81.20%  75  18.80%  11  100.00%
South Yorkshire 2   100.00% 0   0.00%   218 80.40%  53  19.60%  18  90.00%  2   10.00%  44  95.70%  2   4.30%   9   64.30%  5   35.70%  350 95.90%  15  4.10%   9   81.80%  2   18.20%  75  91.50%  7   8.50%   114 95.00%  6   5.00%   78  84.80%  14  15.20%  41  85.40%  7   14.60%  162 89.50%  19  10.50%  13  100.00%
Staffordshire   0   -   0   -   150 69.40%  66  30.60%  15  88.20%  2   11.80%  20  95.20%  1   4.80%   8   80.00%  2   20.00%  161 85.60%  27  14.40%  11  91.70%  1   8.30%   55  74.30%  19  25.70%  80  93.00%  6   7.00%   85  81.70%  19  18.30%  63  90.00%  7   10.00%  147 83.10%  30  16.90%  8   100.00%
Suffolk 1   100.00% 0   0.00%   138 87.30%  20  12.70%  7   100.00% 0   0.00%   14  73.70%  5   26.30%  4   80.00%  1   20.00%  111 95.70%  5   4.30%   5   83.30%  1   16.70%  28  90.30%  3   9.70%   40  97.60%  1   2.40%   61  91.00%  6   9.00%   43  86.00%  7   14.00%  68  88.30%  9   11.70%  5   100.00%
Surrey  0   -   0   -   93  75.60%  30  24.40%  11  61.10%  7   38.90%  5   62.50%  3   37.50%  8   88.90%  1   11.10%  111 88.80%  14  11.20%  17  94.40%  1   5.60%   23  82.10%  5   17.90%  55  96.50%  2   3.50%   39  83.00%  8   17.00%  37  71.20%  15  28.80%  134 87.60%  19  12.40%  17  100.00%
Sussex  0   -   0   -   171 80.30%  42  19.70%  15  65.20%  8   34.80%  35  87.50%  5   12.50%  7   100.00% 0   0.00%   190 91.80%  17  8.20%   14  73.70%  5   26.30%  36  85.70%  6   14.30%  80  95.20%  4   4.80%   72  84.70%  13  15.30%  74  72.50%  28  27.50%  161 89.40%  19  10.60%  17  100.00%
Thames Valley   9   100.00% 0   0.00%   214 65.20%  114 34.80%  26  72.20%  10  27.80%  40  87.00%  6   13.00%  10  62.50%  6   37.50%  296 92.50%  24  7.50%   15  83.30%  3   16.70%  51  82.30%  11  17.70%  122 93.10%  9   6.90%   105 84.00%  20  16.00%  32  76.20%  10  23.80%  280 89.70%  32  10.30%  40  100.00%
Warwickshire    0   -   0   -   35  76.10%  11  23.90%  7   87.50%  1   12.50%  10  100.00% 0   0.00%   1   100.00% 0   0.00%   76  96.20%  3   3.80%   1   100.00% 0   0.00%   10  83.30%  2   16.70%  18  94.70%  1   5.30%   15  78.90%  4   21.10%  32  91.40%  3   8.60%   154 90.60%  16  9.40%   0   -
West Mercia 1   50.00%  1   50.00%  156 78.80%  42  21.20%  24  85.70%  4   14.30%  14  82.40%  3   17.60%  5   83.30%  1   16.70%  169 93.40%  12  6.60%   5   100.00% 0   0.00%   47  92.20%  4   7.80%   55  98.20%  1   1.80%   66  86.80%  10  13.20%  49  89.10%  6   10.90%  198 88.80%  25  11.20%  3   100.00%
West Midlands   6   66.70%  3   33.30%  413 64.90%  223 35.10%  28  75.70%  9   24.30%  90  82.60%  19  17.40%  55  77.50%  16  22.50%  558 92.20%  47  7.80%   35  97.20%  1   2.80%   109 83.20%  22  16.80%  226 92.60%  18  7.40%   116 77.90%  33  22.10%  164 79.60%  42  20.40%  304 86.10%  49  13.90%  16  100.00%
West Yorkshire  1   100.00% 0   0.00%   420 76.60%  128 23.40%  40  83.30%  8   16.70%  78  80.40%  19  19.60%  30  93.80%  2   6.30%   505 93.70%  34  6.30%   22  88.00%  3   12.00%  86  81.90%  19  18.10%  173 95.60%  8   4.40%   116 80.00%  29  20.00%  52  83.90%  10  16.10%  297 85.10%  52  14.90%  35  100.00%
Wiltshire   1   100.00% 0   0.00%   74  75.50%  24  24.50%  9   69.20%  4   30.80%  6   100.00% 0   0.00%   3   100.00% 0   0.00%   90  92.80%  7   7.20%   4   80.00%  1   20.00%  26  83.90%  5   16.10%  25  100.00% 0   0.00%   22  81.50%  5   18.50%  13  86.70%  2   13.30%  79  85.90%  13  14.10%  10  100.00%

我希望在一个地方获得一个包含所有月份的单个文件,这样我就可以对其进行数据分析。

【问题讨论】:

  • 欢迎来到 SO,如果您希望获得帮助,您的帖子需要一些改进。请阅读How to Ask 并写一个minimal reproducible example。此时社区将不得不猜测。因为没有输入样本,所以没有预期的输出定义。
  • 你的合并代码“什么都不返回”是什么意思?示例代码中没有“return”语句。您是否验证了最初从磁盘读取的每个部分中的记录数?在combined_csv 中?在下降()之后?写入磁盘?请更新每个阶段的记录数,并说明问题所在。
  • @SarahMesser 是的,这是正确的,但它应该输出到一个文件(“principal_offence_year_2014.csv”)并且它不这样做。此外,之前的 44 条和之后的 44 条记录的数量完全相同,但由于它的输出方式,我看不出它是否真的进行了任何更改。做单列时效果很好,但完整的代码会返回整个文件,所以我实际上看不到更改。
  • 请发布数据样本,以及所需的输出。
  • @RJAdriaansen 我基本上是在清理空白空间的数据集,准备完成散点图,并推断相关性。使用一张年表中的所有月度数据,这个过程会容易得多吗?

标签: python pandas csv merge data-analysis


【解决方案1】:

确保将.csv 添加到输出文件名的末尾。

如果您想将此与上一个问题结合起来,您可以执行以下操作:

import pandas as pd

parts = [
    "principal_offence_category_april_2014.csv", 
    "principal_offence_category_august_2014.csv",
    "principal_offence_category_december_2014.csv",
    "principal_offence_category_february_2014.csv",
    "principal_offence_category_january_2014.csv",
    "principal_offence_category_july_2014.csv",
    "principal_offence_category_june_2014.csv",
    "principal_offence_category_march_2014.csv",
    "principal_offence_category_may_2014.csv",
    "principal_offence_category_november_2014.csv",
    "principal_offence_category_october_2014.csv",
    "principal_offence_category_september_2014.csv",
]

dfs = [pd.read_csv(part, thousands=',', na_values=['-', '--']) for part in parts]
df_combined = pd.concat(dfs)
df_combined.columns = [f"V{i}" for i in range(1, len(df_combined.columns)+1)]

for col in df_combined.columns[1:]:
    if df_combined[col].dtype == type(str):
        df_combined[col] = df_combined[col].str.rstrip('%').astype('float')

df_combined.to_csv("principal_offence_year_2014_output.csv", index=False)    

【讨论】:

    猜你喜欢
    • 2020-09-03
    • 1970-01-01
    • 2016-06-30
    • 2017-12-17
    • 2021-03-23
    • 2020-03-18
    • 2017-06-07
    • 2012-08-12
    • 1970-01-01
    相关资源
    最近更新 更多