Question

22.39 LAB 13 C FALL 2019 Overview Demonstrate your ability to use pandas with functions Description Write a program that read
King and Program result of the head method • Display the subset of data Your function returns the subset of data. Capture tha
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#Version: Python3

#In-built packages
import os

#External packages
import pandas as pd    # pip3 install pandas


def get_subset(data_frame, column1, column2):
   """
   Gives the subset of a csv file with specified column names only (5rows x 2columns).

   Parameters:
       data_frame = Pandas' DataFrame of an input csv file.
       column1 = Selected 1st column to display.
       column2 = Selected 2nd column to display.

   Returns:
       First five(5) rows of the two columns specified.

   Exception:
       If any of the specified column(s) is not there in the data_frame.
   """
   try:
       # head() is for getting only first 5 rows
       return (data_frame[[column1, column2]]).head()
   except Exception as e:
       print ('Specified column(s) are not found')

if __name__ == '__main__':
   csv_file = input('Enter a file name: ')
   # Checking if the input file is a csv or not
   if csv_file.endswith('csv'):
       # Checking if the input file exists or not
       if os.path.exists(csv_file):
           # Reading csv using pandas library
           data_frame = pd.read_csv(csv_file, skipinitialspace=True)
           column1 = input('Enter the first column to display: ')
           column2 = input('Enter the second column to display: ')
           # Assigning get_subset() function value to subset
           subset = get_subset(data_frame, column1, column2)
           print ('First 5 rows of columns %s and %s:' %(column1, column2))
           # Printing the final output of 5rows x 2columns
           print (subset)
       else:
           print ('Specified file is not found')
   else:
       print('Error: Give only csv file')
else:
   print ('The function is called by another program')

Add a comment
Know the answer?
Add Answer to:
22.39 LAB 13 C FALL 2019 Overview Demonstrate your ability to use pandas with functions Description...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Lab Exercise #15 Assignment Overview This lab exercise provides practice with Pandas data analysis library. Data...

    Lab Exercise #15 Assignment Overview This lab exercise provides practice with Pandas data analysis library. Data Files We provide three comma-separated-value file, scores.csv , college_scorecard.csv, and mpg.csv. The first file is list of a few students and their exam grades. The second file includes data from 1996 through 2016 for all undergraduate degree-granting institutions of higher education. The data about the institution will help the students to make decision about the institution for their higher education such as student completion,...

  • 23.4 PROJECT 4: Using Pandas for data analysis and practice with error handling Overview In this...

    23.4 PROJECT 4: Using Pandas for data analysis and practice with error handling Overview In this project, you will use the Pandas module to analyze some data about some 20th century car models, country of origin, miles per gallon, model year, etc. Provided Input Files An input file with nearly 200 rows of data about automobiles. The input file has the following format (the same as what you had for your chapter 13 labs). The following is an example of...

  • 23.4 Project 4: Using Pandas for data analysis and practice with error handling Python Please! 23.4...

    23.4 Project 4: Using Pandas for data analysis and practice with error handling Python Please! 23.4 PROJECT 4: Using Pandas for data analysis and practice with error handling Overview In this project, you will use the Pandas module to analyze some data about some 20th century car models, country of origin, miles per gallon, model year, etc. Provided Input Files An input file with nearly 200 rows of data about automobiles. The input file has the following format (the same...

  • (a) Load the data file data/tips.csv into a pandas DataFrame called tips_df using the pandas read_table()...

    (a) Load the data file data/tips.csv into a pandas DataFrame called tips_df using the pandas read_table() function. Check the first five rows. (b) Create a new dataframe called tips by randomly sampling 6 records from the dataframe tips_df. Refer to the sample() function documentation. (c) Add a new column to tips called idx as a list ['one', 'two', 'three', 'four', 'five', 'six'] and then later assign it as the index of tips dataframe. Display the dataframe. (d) Create a new...

  • 16.43 Lab 13C: Palindromes with Files and Functions Overview This is a demonstration of reading and...

    16.43 Lab 13C: Palindromes with Files and Functions Overview This is a demonstration of reading and writing files, along with using user-defined functions. Objectives Be able to read from an input file, perform string manipulation on each line of the file within a function, and write to an output file. Provided input file: A single input file named myinput.txt is provided that contains a few lines of text. bob sees over the moon never odd or even statistics dr awkward...

  • Hi - Some help create a short program that uses a pandas DataFrame (solution MUST use...

    Hi - Some help create a short program that uses a pandas DataFrame (solution MUST use a DataFrame) to do the following. Please include comments /notes to explain what is going on. Solution does not need to define functions two csv files are included (grades01.csv): raw marks of students taking an examination; and a further csv file (rubric01.csv) summarizing the maximum mark available for each of the six questions. Structure of grades01: Student ID,Question 1,Question 2,Question 3,Question 4,Question 5,Question 6...

  • Python Assignment In this assignment, you will use Pandas library to perform analysis on the dataset stored in the following csv file: breast-cancer-wisconsin.csv. Please write script(s) to do the fol...

    Python Assignment In this assignment, you will use Pandas library to perform analysis on the dataset stored in the following csv file: breast-cancer-wisconsin.csv. Please write script(s) to do the following: 1. Read the csv file and covert the dataset into a DataFrame object. 2. Persist the dataset into a SQL table and a JASON file. • Write the content of the DataFrame object into an SQLite database table. This will convert the dataset into a SQL table format. You can...

  • Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a funct...

    Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a function that prompts the user for the name of a file to output as a text file that will hold a two dimensional array of the long double data type. Have the user specify the number of rows and the number of columns for the two dimensional array. Have the user enter the values for each row and column element in...

  • Write a menu based program implementing the following functions: (0) Write a function called displayMenu that...

    Write a menu based program implementing the following functions: (0) Write a function called displayMenu that does not take any parameters, but returns an integer representing your user's menu choice. Your program's main function should only comprise of the following: a do/while loop with the displayMenu function call inside the loop body switch/case, or if/else if/ ... for handling the calls of the functions based on the menu choice selected in displayMenu. the do/while loop should always continue as long...

  • This assignment uses functions, files, and strings. Enough flexibility is provided for you to apply your...

    This assignment uses functions, files, and strings. Enough flexibility is provided for you to apply your knowledge of basic C++ programing to develop your solution. Develop a functional flowchart and then write a C++ program to solve the following problem. 1. Create a text file named file1.txt and write your brand of car (like Honda, Toyota, etc) in the file. You will be reading the name of the file from the keyboard as a string, using the string class. Your...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT