Question

In: Computer Science

"Using Python, code to find the smallest and largest planet mass (if known), and plot these...

"Using Python, code to find the smallest and largest planet mass (if known), and plot these as two curves against the year of discovery"

Basically looking to use data from an excel sheet where 'disc_year' and 'pl_mass' are columns of data; and code Python to find the maximum value of mass and the minimum value of mass for each year, then plot this as two curves. There are multiple planets for any given year hence multiple values for mass. Below is example of what data might be.

Example data: Mass - 5.5, 0.2, 6, 56, 209, 0.3, 44, 124, 403, 98, 304, 202, 11.7, 5.4, 17.8, 21.9, 603.3, 108.2, 19, 0.3

Year - 2019, 2019, 1994, 2002, 2001, 2016, 1994, 2019, 2002, 2001, 2016, 2015, 2015, 1999, 2002, 1999, 1999, 2015, 2001, 2016

Solutions

Expert Solution

import pandas as pd

# creating a dataframe

df = pd.DataFrame([(5.5,2019),

(0.2,2019),

(6, 1994),

(56,2002),

(209,2001),

(0.3,2016),

(44,1994),

(124,2019),

(403,2002),

(98,2001),

(304,2016),

(202,2015),

(11.7,2015),

(5.4,1999),

(17.8,2002),

(21.9,1999),

(603.3,1999),

(108.2,2015),

(19,2001),

(0.3,2016)],

columns =('pl_mass', 'disc_year'))

print(df)

grouped_df = df.groupby("disc_year") #group by disc_year

maximums = grouped_df.max() #finding maximum value of pl_mass

maximums = maximums.reset_index() #resetting index and creating a dataframe for maximum pl_mass with year for plotting

print(maximums)

grouped_df = df.groupby("disc_year") #group by disc_year

minimums = grouped_df.min() #finding mainimum value of pl_mass

minimums = minimums.reset_index() #resetting index and creating a dataframe for minimum pl_mass with year for plotting

print(minimums)

import matplotlib.pyplot as plt

# line 1 points

x1 = maximums["disc_year"]

y1 = maximums["pl_mass"]

# plotting the line 1 points

plt.plot(x1, y1, label = "Maximum")

# line 2 points

x2 = minimums["disc_year"]

y2 = minimums["pl_mass"]

# plotting the line 2 points

plt.plot(x2, y2, label = "Minimum")

# naming the x axis

plt.xlabel('Discovery Year')

# naming the y axis

plt.ylabel('PLT_MASS')

# giving a title to my graph

plt.title('Comparison Curve')

# show a legend on the plot

plt.legend()

# function to show the plot

plt.show()

To use data directly form excel sheet you can try the following piece of code :

Import pandas as pd

Df = pd.real_excel("name_of_file.xlsx")

Or directly create a data frame as shown in the above code.


Related Solutions

Summary In this lab, you complete a prewritten Python program that computes the largest and smallest...
Summary In this lab, you complete a prewritten Python program that computes the largest and smallest of three integer values. The three values are -50, 53, 78. Instructions Two variables named largestand smallest are assigned for you. Use these variables to store the largest and smallest of the three integer values. You must decide what other variables you will need and initialize them if appropriate. Write the rest of the program using assignment statements, if statements, or elifstatements as appropriate....
Python - You are given a sorted (from smallest to largest) array A of n distinct...
Python - You are given a sorted (from smallest to largest) array A of n distinct integers which can be positive, negative or zero. Design the fastest algorithm you can for deciding if there is an index i such that A[i] = i.
1. in the code below, 2 variables (largest and smallest) are declared. use these variables to...
1. in the code below, 2 variables (largest and smallest) are declared. use these variables to store the largest and smallest of three integer values. you must decide what other variables you will need and initialize them if appropriate. 2. write the rest of the program using assignment statements, if statements, or if else statements as appropriate. There are comments in the code that tell you where you should write your statements 3. compile and execute. Output should be: The...
Develop a recursive algorithm to find the smallest and largest element in an array and trace...
Develop a recursive algorithm to find the smallest and largest element in an array and trace the recursive function with appropriate message. using c++ add comment to the code
The largest satellite of the dwarf planet Pluto is Charon which has a mass of 1.586x...
The largest satellite of the dwarf planet Pluto is Charon which has a mass of 1.586x 10^22 kg and a 1212 km diameter. Charon is being visited by the intrepid geologist astronaut, Lil Suzi. Glad that her long trip from Ceres is over, in celebration she pitched a 3 kg piece of Charon’s ice horizontally at 22 meters per second over the edge of serendipity chasma (a canyon) at a location where the canyon is 1560 meters deep. A) if...
• Write a C++ program to find the largest umber, smallest number and sum of all...
• Write a C++ program to find the largest umber, smallest number and sum of all the element of a given array of 20 integers • Note − Declare array to 20 numbers − Input values for 20 array elements − Find largest number − Find smallest number − Find sum of all numbers • Display the results
Write two algorithms to find both the smallest and largest numbers in a list of n...
Write two algorithms to find both the smallest and largest numbers in a list of n numbers. In first algorithm, you simply write one loop to find the minimum and maximum. So there will be 2(n - 1) comparisons. In second algorithm, you try to find a method that does at most 1.5n comparisons of array items. Determine the largest list size (i.e., n) that Algorithm 1 can process and still compute the answer within 60 seconds. Report how long...
Write a Python program to find the smallest positive integer that is a perfect square and...
Write a Python program to find the smallest positive integer that is a perfect square and it contains exactly three different digits.
Let A and B be two events. Find the largest and smallest possible values P(A U...
Let A and B be two events. Find the largest and smallest possible values P(A U B) can take in terms of P(A) and P(B) and give examples in which these values can be attained.
In Java Find the second largest and second smallest element in a given array. You can...
In Java Find the second largest and second smallest element in a given array. You can hardcode/declare the array in your program.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT