In: Computer Science
***PYTHON***
CSV:
OrderDate | Region | Rep | Item | 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:
The CSV data is provided above. Please use pandas and python for this question and provide the code.
The program is given below:
a) below code is used to import csv in python
import pandas as pd
#import the CSV in python
df = pd.read_csv (r'read_data.csv')
df1=df.head(13)
print(df1)
The read_data.csv
OrderDate | Region | Rep | Item | Units | Unit Cost | Total |
09-01-2014 | Central | Smith | Desk | 2 | 125 | 250 |
6/17/2015 | Central | Kivell | Desk | 5 | 125 | 625 |
09-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-05-2014 | Central | Morgan | Binder | 28 | 8.99 | 251.72 |
12/21/2015 | Central | Andrews | Binder | 28 | 4.99 | 139.72 |
02-09-2014 | Central | Jardine | Pencil | 36 | 4.99 | 179.64 |
08-07-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.976 | 20.309 | 443.082 | |||
Median | 51.5 | 4.99 | 255.84 | |||
Mode | 7 | 4.99 | 499.1 |
Output:
b) below code is used to create a new variable (column), which is the ratio of Unit Cost divided by Total.
#create a new variable(column), which is the ratio of Unit Cost
divided by Total.
Cost_total_ratio=[]
for index, row in df1.iterrows():
a=(float(row["Unit Cost"]))
b=float(float(row["Total"].replace(',','')))
Cost_total_ratio.insert(index,a/b)
df1['Cost Total Ratio'] = Cost_total_ratio
c) The below code is used to Print the new variable(Cost Total Ratio) values.
df1['Cost Total Ratio']
Output: