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 985793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:13:05+00:00 2026-05-16T05:13:05+00:00

This is an elaboration of a previous question, but as I delve deeper into

  • 0

This is an elaboration of a previous question, but as I delve deeper into python, I just get more confused as to how python handles csv files.

I have a csv file, and it must stay that way (e.g., cannot convert it to text file). It is the equivalent of a 5 rows by 11 columns array or matrix, or vector.

I have been attempting to read in the csv using various methods I have found here and other places (e.g. python.org) so that it preserves the relationship between columns and rows, where the first row and the first column = non-numerical values. The rest are float values, and contain a mixture of positive and negative floats.

What I wish to do is import the csv and compile it in python so that if I were to reference a column header, it would return its associated values stored in the rows. For example:

>>> workers, constant, age
>>> workers
    w0
    w1
    w2
    w3
    constant
    7.334
    5.235
    3.225
    0
    age
    -1.406
    -4.936
    -1.478
    0

And so forth…

I am looking for techniques for handling this kind of data structure. I am very new to python.

  • 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-05-16T05:13:06+00:00Added an answer on May 16, 2026 at 5:13 am

    Python’s csv module handles data row-wise, which is the usual way of looking at such data. You seem to want a column-wise approach. Here’s one way of doing it.

    Assuming your file is named myclone.csv and contains

    workers,constant,age
    w0,7.334,-1.406
    w1,5.235,-4.936
    w2,3.2225,-1.478
    w3,0,0
    

    this code should give you an idea or two:

    >>> import csv
    >>> f = open('myclone.csv', 'rb')
    >>> reader = csv.reader(f)
    >>> headers = next(reader, None)
    >>> headers
    ['workers', 'constant', 'age']
    >>> column = {}
    >>> for h in headers:
    ...    column[h] = []
    ...
    >>> column
    {'workers': [], 'constant': [], 'age': []}
    >>> for row in reader:
    ...   for h, v in zip(headers, row):
    ...     column[h].append(v)
    ...
    >>> column
    {'workers': ['w0', 'w1', 'w2', 'w3'], 'constant': ['7.334', '5.235', '3.2225', '0'], 'age': ['-1.406', '-4.936', '-1.478', '0']}
    >>> column['workers']
    ['w0', 'w1', 'w2', 'w3']
    >>> column['constant']
    ['7.334', '5.235', '3.2225', '0']
    >>> column['age']
    ['-1.406', '-4.936', '-1.478', '0']
    >>>
    

    To get your numeric values into floats, add this

    converters = [str.strip] + [float] * (len(headers) - 1)
    

    up front, and do this

    for h, v, conv in zip(headers, row, converters):
      column[h].append(conv(v))
    

    for each row instead of the similar two lines above.

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

Sidebar

Related Questions

An elaboration on this question , but with more constraints. The idea is the
This is an elaboration on this question: c# Enum Function Parameters I created a
My question is an elaboration of this one: PHP/MYSQL AJAX Chat I have a
this is my first question in here, and I would like to ask if
This question is directly related to this SO question I posed about 15 minutes
This question is kind of a follow up to this question I asked a
Using SQL Server 2008. This is a really junior question and I could really
There's too much elaboration about the HTTP protocol. But to its essensce, it's nothing
I'll try to explain this as best as i can, but i appologize if
This simplified script is enough to cause the issue... just checking if the '-d'

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.