Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

120
Views
Count how many occurrences of a word are there in CSV file

I have a csv file with data like this

PassengerId,Pclass,Name,Sex,Age,SibSp,Parch,Ticket,Fare,Cabin,Embarked
111,1,"Ken, Mr. James",male,34.5,0,0,330911,7.8292,,A
112,2,"Will, Mrs. James",female,47,1,0,363272,7,,B
and so on 

I want to count how many of these names in the csv file include Mr and Mrs and other

and using column 'sex' I want to print the number of male and female passengers.

I have tried this but it just prints every line with 0 for Mrs and 1 for Mr instead of giving me the count for Names with Mr.

I dont know how to loop through the csv data, can someone add the code part for that. Thanks.

<script src='https://d3js.org/d3.v7.min.js'></script>

<script>
            let mycsv = ' some file name';

d3.csv(mycsv, function(data) {
let count = 0;
var theWord  = "Mr";

    if (data.Name.includes(theWord))
    {
        count++;
    }
        
console.log(count);
});

</script>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

  1. You need then from version 5+

    let mycsv = 'example.csv';
    d3.csv(mycsv)
    .then(function(data) {
      /* process data */
    })
    
  2. You need to loop.

That can be done with a reduce:

https://plungjan.name/SO/d3csv/

const process = data =>  {
  const counts = data.reduce((acc, { Name, Sex, Age }) => {
    acc.mr     += Name.toLowerCase().includes("mr.");
    acc.mrs    += Name.toLowerCase().includes("mrs.");
    acc.male   += Sex.toLowerCase() === "male";
    acc.female += Sex.toLowerCase() === "female";
    acc.age    += +Age
    return acc;
  }, { mr: 0, mrs: 0, male: 0, female: 0, age: 0 });
  
  console.log("Total age",counts.age)
  
  Object.entries(counts).forEach(([key,val]) => document.getElementById(key).textContent = val)
}

// uncomment when you want to read a file

// let mycsv = 'example.csv';
// d3.csv(mycsv)
//.then(function(data) {
  process(data)
//})
<script src='https://d3js.org/d3.v7.min.js'></script>
<script>
  const data = d3.csvParse(`PassengerId,Pclass,Name,Sex,Age,SibSp,Parch,Ticket,Fare,Cabin,Embarked
111,1,"Ken, Mr. James",male,34.5,0,0,330911,7.8292,,A
112,2,"Will, Mrs. James",female,47,1,0,363272,7,,B`);
</script>

Mr: <span id="mr"></span><br/>
Mrs: <span id="mrs"></span><br/>
Male: <span id="male"></span><br/>
Female: <span id="female"></span><br/>
Sum of Ages <span id="age"></span><br/>

If you JUST want to count ppl with age above 30:

const process = data =>  {
  const ages = data.map(({Age}) => +Age) // make sure it is a number
  .filter(num => num > 30).length

  console.log("Total of ppl with age above 30:",ages)
}

// uncomment when you want to read a file

// let mycsv = 'example.csv';
// d3.csv(mycsv)
//.then(function(data) {
  process(data)
//})
<script src='https://d3js.org/d3.v7.min.js'></script>
<script>
  const data = d3.csvParse(`PassengerId,Pclass,Name,Sex,Age,SibSp,Parch,Ticket,Fare,Cabin,Embarked
111,1,"Ken, Mr. James",male,34.5,0,0,330911,7.8292,,A
112,2,"Will, Mrs. James",female,47,1,0,363272,7,,B`);
</script>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!