Debian Codegraph @

docs/tutorial/2_examining_the_data.rst

raw
1 ==================
2 Examining the data
3 ==================
4
5 csvstat: statistics without code
6 ================================
7
8 In the previous section we saw how we could use :doc:`/scripts/csvlook` and :doc:`/scripts/csvcut` to view slices of our data. This is a good tool for exploring a dataset, but in practice we usually need to get the broadest possible view before we can start diving into specifics.
9
10 :doc:`/scripts/csvstat` is designed to give us just such a broad understanding of our data. Inspired by the ``summary()`` function from the computational statistics programming language `"R" <https://www.r-project.org/>`_, :doc:`/scripts/csvstat` will generate summary statistics for all the data in a CSV file.
11
12 Let's examine summary statistics for a few columns from our dataset. As we learned in the last section, we can use :doc:`/scripts/csvcut` and a pipe to pick out the columns we want:
13
14 .. code-block:: console
15
16 $ csvcut -c county,acquisition_cost,ship_date data.csv | csvstat
17 1. "county"
18
19 Type of data: Text
20 Contains null values: False
21 Unique values: 35
22 Longest value: 10 characters
23 Most common values: DOUGLAS (760x)
24 DAKOTA (42x)
25 CASS (37x)
26 HALL (23x)
27 LANCASTER (18x)
28
29 2. "acquisition_cost"
30
31 Type of data: Number
32 Contains null values: False
33 Unique values: 75
34 Smallest value: 0
35 Largest value: 412,000
36 Sum: 5,430,787.55
37 Mean: 5,242.073
38 Median: 6,000
39 StDev: 13,368.078
40 Most common values: 6,800 (304x)
41 10,747 (195x)
42 6,000 (105x)
43 499 (98x)
44 0 (81x)
45
46 3. "ship_date"
47
48 Type of data: Date
49 Contains null values: False
50 Unique values: 84
51 Smallest value: 2006-03-07
52 Largest value: 2014-01-30
53 Most common values: 2013-04-25 (495x)
54 2013-04-26 (160x)
55 2008-05-20 (28x)
56 2012-04-16 (26x)
57 2006-11-17 (20x)
58
59 Row count: 1036
60
61 :doc:`/scripts/csvstat` infers the type of data in each column and then performs basic statistics on it. The particular statistics computed depend on the type of the column (numbers, text, dates, etc).
62
63 In this example the first column, ``county`` was identified as type ``Text``. We see that there are ``35`` counties represented in the dataset and that ``DOUGLAS`` is far and away the most frequently occurring. A quick Google search shows that there are ``93`` counties in Nebraska, so we know that either not every county received equipment or that the data is incomplete. We can also find out that Douglas county contains Omaha, the state's largest city by far.
64
65 The ``acquisition_cost`` column is type ``Number``. We see that the largest individual cost was ``412000.0``. (Probably dollars, but let's not presume.) Total acquisition costs were ``5430787.55``.
66
67 Lastly, the ``ship_date`` column (type ``Date``) shows us that the earliest data is from ``2006`` and the latest from ``2014``. We may also note that an unusually large amount of equipment was shipped in April, 2013.
68
69 As a journalist, this quick glance at the data gave me a tremendous amount of information about the dataset. Although we have to be careful about assuming too much from this quick glance (always double-check the numbers mean what you think they mean!) it can be an invaluable way to familiarize yourself with a new dataset.
70
71 csvgrep: find the data you need
72 ===============================
73
74 After reviewing the summary statistics you might wonder what equipment was received by a particular county. To get a simple answer to the question we can use :doc:`/scripts/csvgrep` to search for the state's name amongst the rows. Let's also use :doc:`/scripts/csvcut` to just look at the columns we care about and :doc:`/scripts/csvlook` to format the output:
75
76 .. code-block:: console
77
78 $ csvcut -c county,item_name,total_cost data.csv | csvgrep -c county -m LANCASTER | csvlook
79 | county | item_name | total_cost |
80 | --------- | ------------------------------ | ---------- |
81 | LANCASTER | RIFLE,5.56 MILLIMETER | 120 |
82 | LANCASTER | RIFLE,5.56 MILLIMETER | 120 |
83 | LANCASTER | RIFLE,5.56 MILLIMETER | 120 |
84 | LANCASTER | RIFLE,5.56 MILLIMETER | 120 |
85 | LANCASTER | RIFLE,5.56 MILLIMETER | 120 |
86 | LANCASTER | RIFLE,5.56 MILLIMETER | 120 |
87 | LANCASTER | RIFLE,5.56 MILLIMETER | 120 |
88 | LANCASTER | RIFLE,5.56 MILLIMETER | 120 |
89 | LANCASTER | RIFLE,5.56 MILLIMETER | 120 |
90 | LANCASTER | RIFLE,5.56 MILLIMETER | 120 |
91 | LANCASTER | LIGHT ARMORED VEHICLE | 0 |
92 | LANCASTER | LIGHT ARMORED VEHICLE | 0 |
93 | LANCASTER | LIGHT ARMORED VEHICLE | 0 |
94 | LANCASTER | MINE RESISTANT VEHICLE | 412,000 |
95 | LANCASTER | IMAGE INTENSIFIER,NIGHT VISION | 6,800 |
96 | LANCASTER | IMAGE INTENSIFIER,NIGHT VISION | 6,800 |
97 | LANCASTER | IMAGE INTENSIFIER,NIGHT VISION | 6,800 |
98 | LANCASTER | IMAGE INTENSIFIER,NIGHT VISION | 6,800 |
99
100 ``LANCASTER`` county contains Lincoln, Nebraska, the capital of the state and its second-largest city. The ``-m`` flag means "match" and will find text anywhere in a given column--in this case the ``county`` column. For those who need a more powerful search you can also use ``-r`` to search for a regular expression.
101
102 csvsort: order matters
103 ======================
104
105 Now let's use :doc:`/scripts/csvsort` to sort the rows by the ``total_cost`` column, in reverse (descending) order:
106
107 .. code-block:: console
108
109 $ csvcut -c county,item_name,total_cost data.csv | csvgrep -c county -m LANCASTER | csvsort -c total_cost -r | csvlook
110 | county | item_name | total_cost |
111 | --------- | ------------------------------ | ---------- |
112 | LANCASTER | MINE RESISTANT VEHICLE | 412,000 |
113 | LANCASTER | IMAGE INTENSIFIER,NIGHT VISION | 6,800 |
114 | LANCASTER | IMAGE INTENSIFIER,NIGHT VISION | 6,800 |
115 | LANCASTER | IMAGE INTENSIFIER,NIGHT VISION | 6,800 |
116 | LANCASTER | IMAGE INTENSIFIER,NIGHT VISION | 6,800 |
117 | LANCASTER | RIFLE,5.56 MILLIMETER | 120 |
118 | LANCASTER | RIFLE,5.56 MILLIMETER | 120 |
119 | LANCASTER | RIFLE,5.56 MILLIMETER | 120 |
120 | LANCASTER | RIFLE,5.56 MILLIMETER | 120 |
121 | LANCASTER | RIFLE,5.56 MILLIMETER | 120 |
122 | LANCASTER | RIFLE,5.56 MILLIMETER | 120 |
123 | LANCASTER | RIFLE,5.56 MILLIMETER | 120 |
124 | LANCASTER | RIFLE,5.56 MILLIMETER | 120 |
125 | LANCASTER | RIFLE,5.56 MILLIMETER | 120 |
126 | LANCASTER | RIFLE,5.56 MILLIMETER | 120 |
127 | LANCASTER | LIGHT ARMORED VEHICLE | 0 |
128 | LANCASTER | LIGHT ARMORED VEHICLE | 0 |
129 | LANCASTER | LIGHT ARMORED VEHICLE | 0 |
130
131 Two interesting things should jump out about this sorted data: that ``LANCASTER`` county got a very expensive ``MINE RESISTANT VEHICLE`` and that it also go three other ``LIGHT ARMORED VEHICLE``.
132
133 What commands would you use to figure out if other counties also received large numbers of vehicles?
134
135 Summing up
136 ==========
137
138 At this point you should be able to use csvkit to investigate the basic properties of a dataset. If you understand this section, you should be ready to move onto :doc:`3_power_tools`.