Question

In: Computer Science

The table below contains the average daily temperature (C) in two cities: NYC and Philly, recorded...

The table below contains the average daily temperature (C) in two cities: NYC and Philly, recorded during the month of July. Transform the dataset from the wide to the long format. i.e. the new table should have three columns: day, city, and temperature.

import numpy as np

np.random.seed(seed=0)

Cities = ['New York City','Philadelphia']

Temperatures_Data = []

Day = 1

for i in range(100):   

    Daily_Temperatures = [Day]

    for City in Cities:

        Temperature = round(np.random.normal(loc=50,scale=12),2)

        Daily_Temperatures.append(Temperature)

    Temperatures_Data.append(Daily_Temperatures)

    Day += 1

df_columns = ['Day']

for City in Cities:

    df_columns.append(City)

import pandas as pd

Temperatures_Wide_df = pd.DataFrame(data=Temperatures_Data,columns=df_columns)

Temperatures_Wide_df.iloc[0:10]

Solutions

Expert Solution

Python Code to Transform the dataset from the wide to the long format as shown below:-

import numpy as np
import pandas as pd
np.random.seed(seed=0)

Cities = ['New York City','Philadelphia']

Temperatures_Data = []

Day = 1

for i in range(100):   

Daily_Temperatures = [Day]

for City in Cities:

Temperature = round(np.random.normal(loc=50,scale=12),2)

Daily_Temperatures.append(Temperature)

Temperatures_Data.append(Daily_Temperatures)

Day += 1

df_columns = ['Day']

for City in Cities:

df_columns.append(City)


Temperatures_Wide_df = pd.DataFrame(data=Temperatures_Data,columns=df_columns)
Temperatures_Wide_df=Temperatures_Wide_df.iloc[0:10]
df2=pd.melt(Temperatures_Wide_df,id_vars=['Day'],var_name='City', value_name='Temperature')
print(df2)

Output:-


Related Solutions

This worksheet contains data on average annual low temperature and average annual snowfall for select cities....
This worksheet contains data on average annual low temperature and average annual snowfall for select cities. (Note: both of the following tables you are asked to create are more appropriate, given the small number of observations per state, had I created another variable describing the region--NE, SE, MW, Central, and West, for example.) 15) Create frequency distribution summarizing the number of cities included in the sample from each state. You are to use the Pivot Table tool to accomplish this...
The table below gives the average high temperatures in January and July for 12 random cities...
The table below gives the average high temperatures in January and July for 12 random cities in a region with 180 cities. Write a 95​% confidence interval for the mean temperature difference between summer and winter in the region. Mean High Temperatures ​(degrees​F) City A B C D E F G H I J K L July 75 71 77 76 89 87 69 87 73 65 77 84 Jan. 34 36 43 34 56 54 42 46 44 44...
The table below gives the average high temperatures in January and July for 12 random cities...
The table below gives the average high temperatures in January and July for 12 random cities in a region with 192 192 cities. Write a 99 99​% confidence interval for the mean temperature difference between summer and winter in the region. Mean High Temperatures ​( degrees °​F) City A B C D E F G H I J K L July 73 71 78 74 91 88 71 87 74 65 74 85 Jan. 35 37 40 37 56 53...
The table below contains bus fares in 20 different cities 1.2, 1.3, 1.4, 1.6, 1.65, 1.8,...
The table below contains bus fares in 20 different cities 1.2, 1.3, 1.4, 1.6, 1.65, 1.8, 1.9, 2.0, 2.2, 2.25 2.5, 2.6, 2.7, 2.9, 3.0, 3.2, 4.0, 5.0, 8, 10 (b) Carry out a t-test of the hypothesis H0: μ = 3 vs. Ha: μ < 3. Find the p-value and state your conclusions at α = 0.05. ** Please clearly explain how to obtain P-value** This is what is confusing me most.
#4 The numbers in the table below represent the average daily intake of sugar-sweetened soft drinks...
#4 The numbers in the table below represent the average daily intake of sugar-sweetened soft drinks and the average weight for a particular group of men at various times over a period of 40 years. Use that data to answer the questions below. Soda intake (gal) 8 13 18 22 21 27 24 31 31 32 41 44 38 Weight (lb) 173 169 167 169 176 167 184 183 172 172 182 181 192 To two decimal places, the correlation...
In the simple distillation of a mixture of toluene and cyclohexane, the average highest temperature recorded...
In the simple distillation of a mixture of toluene and cyclohexane, the average highest temperature recorded was in 95 degrees Celsius. Explain why in the fractional distillation of the same mixture, the average highest temperature was 108 degrees Celsius.
The table to the below contains observed values and expected values in parentheses for two categorical​...
The table to the below contains observed values and expected values in parentheses for two categorical​ variables, X and​ Y, where variable X has three categories and variable Y has two categories. Use the table to complete parts​ (a) and​ (b) below. X1 X2 X3 Y1 34 40 55 36.86 43.24 48.91 Y2 18 21 14 15.14 17.76 20.09 (a) Compute the value of the​ chi-square test statistic. ​(Round to three decimal places as​ needed.) X 2/0= (b) Test the...
The table below contains a list of species and their abundances for two grassland communities around...
The table below contains a list of species and their abundances for two grassland communities around Serengeti National Park, Tanzania. Use the below data to answer the following question. Community A: Wildlife-Grazed Grassland Number of individuals per m2 Sporobolus ioclados 45 Themeda triandra 22 Dactyloctenium aegyptium 11 Cynodon plectostachyus 17 Harpachne schimperi 5 Community B: Livestock-Grazed Grassland Panicum coloratum 68 Themeda triandra 17 Cynodon dactylon 2 Pennisetum mezianum 10 Eragrostis tenuifolia 3 Using the Shannon diversity index, what is pi...
Let x be a random variable that represents the average daily temperature (in degrees Fahrenheit) in...
Let x be a random variable that represents the average daily temperature (in degrees Fahrenheit) in July in a town in Colorado. The x distribution has a mean μ of approximately 75°F and standard deviation σ of approximately 8°F. A 20-year study (620 July days) gave the entries in the rightmost column of the following table. I         II III IV Region under Normal Curve x°F Expected % from Normal Curve Observed Number of Days in 20 Years μ – 3σ...
The frequency table for daily sales of 100 a sale unit is given below: daily 10...
The frequency table for daily sales of 100 a sale unit is given below: daily 10 - 19 20 - 29 30 – 39 40 - 49 50 - 59 60 - 70 sales No . sale 4 15 27 26 18 10 unit a) Construct a histogram, polygon of daily sales b) Describe the shape of the frequency distribution. c) Draw an ogive of daily sales, and use it to estimate, the median. e) Estimate the following statistical measures...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT