Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

115
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda