Question

***PYTHON*** CSV: OrderDate Region Rep Merchandise Units Unit Cost Total 9/1/2014 Central Smith Desk 2 125...

***PYTHON***

CSV:

OrderDate Region Rep Merchandise Units Unit Cost Total
9/1/2014 Central Smith Desk 2 125 250
6/17/2015 Central Kivell Desk 5 125 625
9/10/2015 Central Gill Pencil 7 1.29 9.03
11/17/2015 Central Jardine Binder 11 4.99 54.89
10/31/2015 Central Andrews Pencil 14 1.29 18.06
2/26/2014 Central Gill Pen 27 19.99 539.73
10/5/2014 Central Morgan Binder 28 8.99 251.72
12/21/2015 Central Andrews Binder 28 4.99 139.72
2/9/2014 Central Jardine Pencil 36 4.99 179.64
8/7/2015 Central Kivell Pen Set 42 23.95 1,005.90
1/15/2015 Central Gill Binder 46 8.99 413.54
1/23/2014 Central Kivell Binder 50 19.99 999.5
9/27/2015 West Sorvino Pen 76 1.99 151.24
Mean 48.97619 20.3086 443.0819
Median 51.5 4.99 255.84
Mode 7 4.99 449.1

Using Python and Pandas complete the following questions:

  1. Import the CSV in python
  2. Within the Merchandise variable, recode Desk as 1, Pencil as 2, Binder as 3, Pen as 4, and Pen Set as 5.
  3. Print the variable to demonstrate the change.

The CSV data is provided above. Please use pandas and python for this question and provide the code.

Homework Answers

Answer #1
  • We will first import pandas library.
  • Then create file handler in read mode for our csv file.
  • Then create a Pandas DataFrame using read_csv function that reads from the csv file.
  • Close the file handler.
  • Then, we will create a dictionary to recode the strings as numbers.
  • Then we will be traversing through dataframe Merchandise column and writing values where key matches.
# import pandas library 
import pandas as pd

# creating file handler  in 
# read mode for  
# our sample.csv file.
file_handler = open("sample.csv", "r")

# creating a Pandas DataFrame 
# using read_csv function  
# that reads from the  sample.csv file. 
data = pd.read_csv(file_handler, sep = ",")

# closing the file handler 
file_handler.close() 

#recode
merchandise = {'Desk':1, 'Pencil':2, 'Binder':3, 'Pen':4, 'Pen Set':5}

#printing variables
print(merchandise)

# traversing through dataframe 
# Merchandise column and writing 
# values where key matches 
data.Merchandise = [merchandise[item] for item in data.Merchandise] 

#print only the Merchandise column.
print(data['Merchandise'])

#print the whole csv file
print(data)

Know the answer?
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
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT