liveBook · Manning

Data cleaning

As explained in D3.js in Action, Third Edition

Data cleaning is the detection and correction or removal of inaccurate, inconsistent, incomplete, or improperly formatted data.

Data cleaning prepares gathered information for visualization by analyzing, cleaning, formatting, and standardizing it. It often takes substantial time and requires patience and diligence. section 14.1 section 9.4

A practical cleaning workflow is:

  • Define what information the visualization should show before searching for data. section 14.1
  • Gather relevant records from different sources, then analyze their structure and content. section 14.1
  • Clean and format the records into a standardized dataset that the visualization can use. section 14.1 section 9.4
  • Define the data scope early so later handling does not become overwhelming. section 9.4

Preparing Van Gogh artwork data

A visualization of Vincent van Gogh’s paintings, drawings, and letters requires records gathered from several resources, analyzed and formatted so they can be situated in time and compared across artistic phases. section 14.1

Listing 14.1 Extracting data from Van Gogh’s paintings on Wikipedia
const tables = [                         #1
  { index: 0, medium: "oil" },          
  { index: 1, medium: "oil" },          
  { index: 2, medium: "oil" },          
  { index: 3, medium: "oil" },          
  { index: 4, medium: "oil" },          
  { index: 5, medium: "oil" },                                           
  { index: 6, medium: "watercolor" },                                    
  { index: 7, medium: "print" },                                         
  { index: 8, medium: "print" },                                         
];                                                                       

const domTables = document.querySelectorAll(     #2
  ".wikitable.sortable"                         
);                                              
const paintings = [];     #3

domTables.forEach((table, i) => {                                        #4
  if (i <= tables.length - 1) {                                        
    const medium = tables[i].medium;                                    
                                                                        
    const rows = table.querySelectorAll("tbody tr");                  
    rows.forEach(row => {                                              
      let title;                                                         #5
      if (row.querySelector(".thumbcaption i a")) {                    
        title = row.querySelector(".thumbcaption i a")                  
          .textContent;                                                 
      } else if (row.querySelector(".thumbcaption i")) {                
        title = row.querySelector(".thumbcaption i")                    
          .textContent;                                                 
      } else {                                                         
        title = null;                                                   
      }                                                                

      let imageLink;                                                     #6
      if (row.querySelector(".thumbinner img")) {                      
        const image = row.querySelector(                                
          ".thumbinner img"                                             
        ).srcset;                                                       
        imageLink = `https${image.slice(                              
          image.indexOf("1.5x, ")+6,-3                                  
        )}`;                                                          
      } else {                                                         
        imageLink = null;                                             
      }                                                               

      paintings.push({                                                  #7
        title: title,                                                 
        imageLink: imageLink,                                          
        medium: medium                                                  
      });                                                               
    })
  }
});

console.log(paintings);                                             #8

A JavaScript listing demonstrates extracting painting records from multiple Wikipedia tables and organizing them by medium before visualization. section 14.1

How D3.js in Action, Third Edition covers it

How deep it goes
Explains scoping, gathering, analysis, cleaning, formatting, and standardization, but stops short of detailed validation rules.
Examples
Uses Van Gogh artwork and letters, Wikipedia tables, and Facebook posts collected through CrowdTangle.
What it assumes
Assumes a defined visualization goal and gathered sources, while explaining preparation from the point of data selection.
What you can do after
Apply it via D3.js by producing a scoped, standardized dataset ready for visualization.
How it connects
Connects data gathering, data preprocessing, data formatting, data visualization, and D3.js.

Also explained in

Compare how 14 books explain data cleaning →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of D3.js in Action, Third Edition. The section links point to the parts of the book it draws on.