Python Forum
Extracting the correct data from a CSV file - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Extracting the correct data from a CSV file (/thread-42241.html)



Extracting the correct data from a CSV file - S2G - Jun-02-2024

Writing a code where I can extract data from this file.
[attachment=2894]
Someone able to help to write a code that can, for example:

"Print all the moons from Jupiter, and rank them from highest to lowest radius?"

or "Print the moons in alphabetic order"

All help apprieciated!


Extracting the correct data from a CSV file - S2G - Jun-02-2024

Writing a code where I can extract data from this file.
[attachment=2894]
Someone able to help to write a code that can, for example:

"Print all the moons from Jupiter, and rank them from highest to lowest radius?"

or "Print the moons in alphabetic order"

All help apprieciated!


RE: Extracting the correct data from a CSV file - menator01 - Jun-02-2024

What have you tried?


RE: Extracting the correct data from a CSV file - S2G - Jun-02-2024

(Jun-02-2024, 08:12 PM)menator01 Wrote: What have you tried?
Pretty new into this, and found using CSV files hard...
But created a list for the relevant moons, but cant figure out how to only append those of Jupiter for a start


RE: Extracting the correct data from a CSV file - deanhystad - Jun-02-2024

What are you finding difficult about reading csv files. This one looks very simple to read using either the csv module or pandas. I suggest pandas.

https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html

Once the csv file is read into a dataframe, you can select rows using conditions.

https://pandas.pydata.org/docs/getting_started/intro_tutorials/03_subset_data.html


RE: Extracting the correct data from a CSV file - Pedroski55 - Jun-03-2024

To sort by radius, I would first change the entries, some of which look like: 1821.6±0.5, 1560.8±0.5

These will give odd results! Change them into float numbers like 1821.6 or 1560.8, to make the sorting easy!

Without getting into pandas for now, as I am very bad in pandas, you can easily get the Jupiter data like this:

import csv
import pandas as pd

path2data = '/home/pedro/myPython/csv/csv/satellites.csv'
moons = []

# put all the data in moons for safe-keeping and further use
with open(path2data) as infile:
    data = csv.reader(infile, delimiter=',')
    for line in data:
        moons.append(line)
        
task1 = "Print all the moons from Jupiter, and rank them from highest to lowest radius."
print(task1)

# just get Jupiter stuff as a list
Jupiter = [p for p in moons if p[0] == 'Jupiter']
len(Jupiter) # returns 67
Now you have a list with all the data on the moons of Jupiter! (Didn't know there were so many!)

For sorting lists, look up sort() and sorted(), for example here.

.sort() and sorted() can be confusing.

The difference between sort() and sorted() is:

Quote:mylist.sort() returns None: sort() sorts the list you are using, for instance here, mylist and so changes it directly.

Quote:sorted(mylist) returns a new list, sorted the way you want.

Using sorted:

mylist = [i for i in 'qwerty']
newlist = sorted(mylist)
mylist # returns ['q', 'w', 'e', 'r', 't', 'y']
newlist # returns ['e', 'q', 'r', 't', 'w', 'y']
Using sort()

mylist = [i for i in 'qwerty']
mylist # returns ['q', 'w', 'e', 'r', 't', 'y']
mylist.sort()
mylist # returns ['e', 'q', 'r', 't', 'w', 'y']
Task 2:

task2 = "Print the moons of Jupiter in alphabetic order"
print(task2)

# Jupiter is a list of lists
# sorted loops through Jupiter and here looks at the second element x[1], the name of the moon, in each list
Jupiter_moons_by_alphabet = sorted(Jupiter, key=lambda x: x[1])
for j in Jupiter_moons_by_alphabet:
    print(j[1])
print('Task 2 done!')
Apparently, people with very good eyesight can actually see the 4 main moons of Jupiter with the naked eye! (No chance there for me!)


RE: Extracting the correct data from a CSV file - snippsat - Jun-03-2024

(Jun-02-2024, 08:07 PM)S2G Wrote: or "Print the moons in alphabetic order"
Just to give help with this task,it's the same start for the other task as you use DataFrame sat_df.
It's a typically dataset to be used with Pandas,you should mention what to use.
As regulars Python and Pandas are quite different.
import pandas as pd

file = 'satellites.csv'
sat_df = pd.read_csv(file)
# Sort the moons alphabetically
moons_sorted = sat_df.sort_values(by='name')
print(moons_sorted[['planet', 'name']])
Output:
planet name 17 Jupiter Adrastea 93 Saturn Aegaeon 107 Saturn Aegir 33 Jupiter Aitne 101 Saturn Albiorix .. ... ... 133 Uranus Titania 151 Uranus Trinculo 158 Neptune Triton 132 Uranus Umbriel 94 Saturn Ymir [177 rows x 2 columns]