Python Forum
Extracting the correct data from a CSV file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Extracting the correct data from a CSV file
#1
Writing a code where I can extract data from this file.

.csv   satellites.csv (Size: 8.59 KB / Downloads: 22)
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!
Reply
#2
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!
Reply
#3
What have you tried?
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#4
(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
Reply
#5
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...d_csv.html

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

https://pandas.pydata.org/docs/getting_s..._data.html
Reply
#6
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!)
Reply
#7
(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]
Pedroski55 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [SOLVED] Correct way to convert file from cp-1252 to utf-8? Winfried 8 1,688 Feb-29-2024, 12:30 AM
Last Post: Winfried
  Extracting specific file from an archive tester_V 4 721 Jan-29-2024, 06:41 PM
Last Post: tester_V
  Extracting Data into Columns using pdfplumber arvin 17 6,417 Dec-17-2022, 11:59 AM
Last Post: arvin
  Extracting Specific Lines from text file based on content. jokerfmj 8 3,339 Mar-28-2022, 03:38 PM
Last Post: snippsat
  Extracting Data from tables DataExtrator 0 1,227 Nov-02-2021, 12:24 PM
Last Post: DataExtrator
  extracting data ajitnayak1987 1 1,654 Jul-29-2021, 06:13 AM
Last Post: bowlofred
  Extracting and printing data ajitnayak1987 0 1,485 Jul-28-2021, 09:30 AM
Last Post: ajitnayak1987
  Extracting unique pairs from a data set based on another value rybina 2 2,444 Feb-12-2021, 08:36 AM
Last Post: rybina
Thumbs Down extracting data/strings from Word doc mikkelibsen 1 2,037 Feb-10-2021, 11:06 AM
Last Post: Larz60+
  Extracting data without showing dtype, name etc. tgottsc1 3 4,936 Jan-10-2021, 02:15 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020