Solutions for Week 10 Pandas FDS

Has any of you finished the Week 10 tasks. Would you like to discuss the solution?

Yes, I have completed the tasks. But Iam not able to do task-8 ( I have found the moving average but iam not understanding how to exclude first month.)
If you know, can you please help me.

1 Like

[Problem No: 8]
I’ve attempted this assuming 30days means 30 working days (other interpretations possible).

Here is my solution:

When you take rolling mean, First 29 entries will automatically be NaN.(based on observation from code)

Please verify the above results.

1 Like

@sanjayk Answer is 140. There is no need to take data backwards. This step can be reduced, still we get same answer.

[Problem No: 8]

Great, Can you share your code as well? It will be helpful.

[Problem No: 3]
This is solution for Problem number 3. I don’t like this solution in particular and would like to learn how others have implemented: any panda magic, some use of groupby etc :slight_smile:

Problem 8
The below code is giving me answer 139

sum(nifty50_2019[“Close”][‘2019-02-01’:] > nifty50_2019[“Close”].rolling(‘30d’).mean()[‘2019-02-01’:])

Is this right?

This is how I have done it.

1 Like

[Problem No 8]

No idea how above will work.

This one is interesting. Thanks for Sharing. :+1:

What will this expression do? (FWIW, the dates run from Dec to Jan)
You can post your output as well.

@krr.prabhu In this code you are calculating moving average from February whereas it should be taken from January and if you change date to January ( in moving average part), You get an error because both two syntax cannot be compared ( as index will not be matched).

Solution - Do not index specifying row. Just do this:

sum(nifty50[‘Close’] > nifty50[‘Close’].rolling(30).mean())

task 4:Compute the mean, median, std, var of closing values for each weekday in NIFTY50 for 2019

I tried grouping weekdays in data in the following way using a timestamp, can someone please help me to know if I am wrong or some another way of grouping?

df_= nifty50.groupby([pd.Timestamp(i).dayofweek for i in nifty50.index[:]])[‘Close’]

This code is successfully group data by weekdays. :+1:

1 Like

Thank you so much. Does there exist another efficient way?

When will the official solutions be discussed?

I’m getting the following answer assuming 30 Working days and not based on months (PS: I’ve excluded first 30 values instead of first month as that makes more sense according to the requirement)

But I’m getting the True values as 104 instead of 139 or 140 as discussed by other students.
Am i doing something differently.

EDIT: I forgot to reverse the order of first DataFrame, which was giving me the wrong answer ultimately, according to me, the answer indeed is 140


I don’t think this code is suppose to give the right answer

I tried all the different variations of this code, and obviously it won’t give the right answer without reversing the order of the DataFrame, as you’d be printing NaNs for the Values of December instead of January.

And I can surely say that the answer to this question is 140

Im a newbiew. Can some one help me verify the solution.

[Question 1]
nifty_data_2019[“Volatile”] = nifty_data_2019[“High”] > (nifty_data_2019[“Low”] * 1.05)

niftyNext_data_2019[“Volatile”] = niftyNext_data_2019[“High”] > (niftyNext_data_2019[“Low”] * 1.05)

1 Like

This is good. One more step. This will create a new column ‘Volatile’ with True and false values. To answer ‘how many volatile days’, you have to count ‘True’ values also. Give it a try.

1 Like