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

  • SEARCH
  • Home
  • 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 8776759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:07:50+00:00 2026-06-13T19:07:50+00:00

I have a large CSV file with many columns, something like this: id, col1,

  • 0

I have a large CSV file with many columns, something like this:

id, col1, col2, col3, col4, col5
1, a, b, 2, d, e
2, b, c, 4, e, f
3, c, d, 6, f, g

I want to be able to create a dictionary in which only certain columns are used. For example, the dictionary would have the id number, col2, and col3. Additionally, it should only store the rows that have the highest 10 numbers in col2. This is the code I have:

import csv 
reader = csv.DictReader(open('SNPs.csv', newline=''), delimiter=',', quotechar='"')

But I do not know how to tell it to ignore certain columns, and I don’t think that I can use max() to return multiple values.

max(2, 4) returns 4. 

EDIT
I tried using Daniel’s Code, but for some reason the sort function isn’t working correctly. (I also need to use reverse sort instead of sort). It only outputs four different keys, and additionally, they aren’t actually sorted in descending numerical order. It also returns the headers as one of the values.

import csv
f = open('SNPs.csv', "rU")
reader = csv.reader(f)
output = [row for row in reader]
output.sort(key=lambda x: x[32], reverse=True)
print dict((row[10], (row[11], row[8], row[32])) for row in output[:10])
  • 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-13T19:07:52+00:00Added an answer on June 13, 2026 at 7:07 pm

    col2 doesn’t have any numbers. I’ll assume you meant col3.

    You can’t tell which are the ten highest numbers in col3 until you’ve read them all. So since you’re going to be doing that anyway, you might as well read everything, then extract the top ten afterwards. So you can do something like this:

    output = []
    for row in reader:
        output.append(dict(k, v) for k, v in row if k in ('id', 'col2', 'col3'))
    output.sort(key=lambda x: x['col3'])
    return output[:10]
    

    Edit Now I see your desired output, you want something completely different to what I imagined. In fact DictReader is completely pointless here, so I’ll rewrite with the normal Reader.

    f = open('SNPs.csv')
    reader = csv.Reader(f, delimiter=',', quotechar='"')
    output = [row for row in reader]
    output.sort(key=lambda x: x[3])
    return dict((row[0], (row[3], row[4])) for row in output[:10])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very large csv file that looks like this: keywords,impressions descargar juegos
I have a large number of csv files that look like this below: xxxxxxxx
I have a large CSV file and I want to execute a stored procedure
I have a large CSV file (7.3GB; 16,300,000 lines), how can I split this
I have a large CSV file and I want to programmatically open it in
This is a part of a large csv file which I have: 66.35.223.128,66.35.223.143,1109647232,1109647247,AU,Australia 66.35.223.144,66.35.227.191,1109647248,1109648319,US,United
I'm parsing a large csv files - about 500 meg (many rows, many columns).
I have a large CSV file (5.4GB) of data. It's a table with 6
I have a large CSV file with the following records: 60,1572236,3,58394247,\N,\N,\N,1925720,\N,2011-11-25 12:55:53,2011-11-25 12:55:53 61,2875109,4,58394209,1234,\N,4025175,\N,\N,2011-11-25
I have a large CSV file, approx 10 MB in size, which contains all

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.