Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7950247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:12:39+00:00 2026-06-04T02:12:39+00:00

I want to select some data out of a CSV file before i load

  • 0

I want to select some data out of a CSV file before i load it with javascript (with the d3 library).

This is how i load the CSV:

d3.csv("data.csv", function(csv) {
    vis.datum(csv).call(chart);
        });

And this is a sample of the CSV file:

Class,Age,Sex,Survived
First Class,Adult,Male,Survived
First Class,Adult,Male,Survived
First Class,Adult,Male,Survived
First Class,Adult,Male,Survived
First Class,Adult,Male,Survived
First Class,Adult,Female,Survived
First Class,Adult,Female,Survived
First Class,Adult,Female,Survived
Second Class,Adult,Male,Perished
Second Class,Adult,Male,Perished
Second Class,Adult,Male,Perished
Third Class,Adult,Male,Survived
Third Class,Adult,Male,Survived
Third Class,Adult,Male,Survived
Third Class,Adult,Male,Survived
Third Class,Adult,Male,Perished
Third Class,Adult,Male,Perished
Crew,Adult,Male,Perished
Crew,Adult,Male,Perished
Crew,Adult,Female,Survived
Crew,Adult,Female,Survived

For example i want only to select the Second Class and First Class rows before i load it with d3.csv.

i know i can just delete the other rows in the CSV, but i want to make a function so that the user can select what categories he want to use. I hope that makes some sense.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T02:12:41+00:00Added an answer on June 4, 2026 at 2:12 am

    The quick answer is, use .filter() to select the rows you want, e.g.:

    d3.csv("data.csv", function(csv) {
        csv = csv.filter(function(row) {
            return row['Class'] == 'Second Class' || row['Class'] == 'First Class';
        })
        vis.datum(csv).call(chart);
    });
    

    This is easy if you, the coder, are choosing the filters. If you need this to be chosen by user interaction, however, you’re going to need to build out a more complex function. Assuming that you have saved the user choices in an object called filters, with keys corresponding to your rows, one option might look like:

    // an example filters object
    var filters = {
        'Class': ['First Class', 'Second Class'],
        'Sex': 'Female'
    };
    
    d3.csv("data.csv", function(csv) {
        csv = csv.filter(function(row) {
            // run through all the filters, returning a boolean
            return ['Class','Age','Sex','Survived'].reduce(function(pass, column) {
                return pass && (
                    // pass if no filter is set
                    !filters[column] ||
                        // pass if the row's value is equal to the filter
                        // (i.e. the filter is set to a string)
                        row[column] === filters[column] ||
                        // pass if the row's value is in an array of filter values
                        filters[column].indexOf(row[column]) >= 0
                    );
            }, true);
        })
        console.log(csv.length, csv);
    });
    

    (You don’t have to do this with .reduce(), but I like how clean it is.)

    If, as is probably the case, you don’t want to do this filtering at load time, but instead filter dynamically depending on user input, you can still use the filter function, but you’ll want to store csv in memory somewhere and filter it on the fly, perhaps in an update() function triggered by user interactions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to get some data out of my database that is similar to
The following code is written to select some data from one file and copy
I want to join 2 tables and get some data out of it using
i have some data in my table when i select that data with this
I have users table and want to SELECT some rows by bitmask criteria. I'll
I have a gtk.Textview . I want to find and select some of the
I want a template to select from two types based on some condition. E.g.
Within CodeIgniter, I'm doing a select, retrieving some blogposts. But I also want the
i have this lists, and i want select li element from jquery when the
I want to select an element which was created by the CSS selector :before

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.