#π Help trying to convert star data into Cartesian coordinates
35 messages Β· Page 1 of 1 (latest)
@pulsar beacon
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.
can you share the csv file?
!paste
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.
remember indices start at 0
so columns 7 & 8 would actually be columns[6] and columns[7] in code
but also, iterating through each row is a bad idea if you want it to run fast
yes i can, is this good?
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()
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
i am using pandas
why not pd.read_csv then?
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
im sorry, im new to this. is this what you mean? im struggling with figuring out how to loop through the data and only get date from the specifc columns 7 & 8 to insert into my equation
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!
im still getting a error, what am i doing wrong?
@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
thanks a lot
is there a way i can get it to loop through every row of data?
how would i do that?
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
thank you
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