0

I am trying to access data from a CSV using python. I am able to access entire columns for data values; however, I want to also access rows, an use like and indexed coordinate system (0,1) being column 0, row 1. So far I have this:

#Lukas Robin 
#25.07.2021
import csv
with open("sun_data.csv") as sun_data:
        sunData = csv.reader(sun_data, delimiter=',')
        global data
        for data in sunData:
            print(data)

I don't normally use data tables or CSV, so this is a new area for me.

1

1 Answer 1

1

As mentioned in the comment, you could make the jump to using pandas and spend a little time learning that. It would be a good investment of time if you plan to do much data analysis or work with data tables regularly.

If you just want to pull in a table of numbers and access it as you request, you are perfectly fine using csv package and doing that. Below is an example...

If your .csv file has a header in it, you can simply add in next(sun_data) before starting the inner loop to advance the iterator and let that data fall on the floor...

import csv
f_in = 'data_table.csv'
data = []  # a container to hold the results
with open(f_in, 'r') as source:
    sun_data = csv.reader(source, delimiter=',')
    for row in sun_data:
        # convert the read-in values to float data types (or ints or ...)
        row = [float(t) for t in row]
        # append it to the data table
        data.append(row)

print(data[1][0])
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, im gonna take the time to learn panda and then ill compare the runtime and use the fastest one
Sure. Your data table would have to be quite huge to have it make an appreciable difference in speed, I think.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.