#πŸ”’ Help trying to convert star data into Cartesian coordinates

35 messages Β· Page 1 of 1 (latest)

pulsar beacon
#

hi, im fairly new to python and coding in general. im running into a error "Index Error: list out of range" whenever i try to run the code. im using data from a csv file and i need to get the data from columns 7 & 8. any advice

fleet elbowBOT
#

@pulsar beacon

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

graceful meadow
#

!paste

fleet elbowBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

stone arrow
#

but also, iterating through each row is a bad idea if you want it to run fast

pulsar beacon
graceful meadow
#

yes, but I'd recommend using a proper csv module for parsing the text

#

cells like this will cause issues if you naively use .split()

stone arrow
#

the second image looks like they're already using pandas or something though
and if this is the case, you shouldn't need to parse line by line yourself at all, a pd.read_csv('path/to/csv') should allow you to load the data into a df directly from the file

pulsar beacon
#

i am using pandas

graceful meadow
#

this code doesn't look like it though

stone arrow
pulsar beacon
stone arrow
# pulsar beacon did i use pd.read_csv right here?

yeah just like that, though you can remove the df = pd.DataFrame(stars, ...) line, pd.read_csv already returns a DataFrame, so stars is already what you need
and you could just use this dataframe instead of parsing line by line again

pulsar beacon
white parrot
#
import csv
import math

with open("example.csv","r") as csvfile:
    reader=csv.reader(csvfile,delimiter=",")
    i=0
    for row in reader:
        if i>0:
            x=math.cos(float(row[8]))*math.cos(float(row[7]))
            y=math.sin(float(row[8]))*math.sin(float(row[7]))
            print(x,y)
        i+=1 
#

@pulsar beacon The above codes solve your problem.

#

@pulsar beacon Remember that line 0 contains the headers, not the numeric values!

pulsar beacon
white parrot
#

@pulsar beacon You made an indentation error. The x, y and print statements are written four spaces after the if block.

#

@pulsar beacon Make the alignments as you see on the screen

pulsar beacon
white parrot
#

@pulsar beacon Don't forget to vote if the problem is solved.

#

yes

pulsar beacon
white parrot
#
import csv

with open("example.csv","r") as csvfile:
    reader=csv.reader(csvfile,delimiter=",")
    i=0
    for row in reader:
        if i>0:
            for number in range(0,len(row)):
                print(number,end=",")
            print("")
            print("="*80)
        i+=1
pulsar beacon
#

thank you

fleet elbowBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.

#

πŸ”’ Help trying to convert star data into Cartesian coordinates