Worst Day - Python for Data Science | Sololearn: Learn to code for FREE!

0

Worst Day - Python for Data Science

Given the COVID data, find the day with maximum cases in a given month. Take a month name as input and output the row that corresponds to the day with the maximum number of cases in that month. You can filter the DataFrame for the given month first, and then select the row with the maximum cases. Important: The output should be a DataFrame, which includes all the columns. My code: ---------------------------------- import pandas as pd df = pd.read_csv("/usercode/files/ca-covid.csv") df.drop('state', axis=1, inplace=True) df['date'] = pd.to_datetime(df['date'], format="%d.%m.%y") df['month'] = df['date'].dt.month_name() df.set_index('date', inplace=True) month = input() newdata = df[(df['month']==month)] data = pd.DataFrame(newdata) print(data.groupby('month')['cases'].max()) ------------------- My output: -------------------- month April 2334 Name: cases, dtype: int64 ----------------------- Expected output: cases deaths month date 2020-04-29 2334 77 April ------------------ Please help, I tried my best but it seems like I can't figure it out how to output the same format as they have. I have used all the elements I learned so far from Pandas to did this. Please help! Thank you

4/13/2021 4:22:11 PM

Alvin Nguyen

7 Answers

New Answer

+4

You are almost there. Just small change https://code.sololearn.com/c43A21a19A22/?ref=app

+1

wow CHANDAN ROY I really appreciate it man. It has some more confusing syntax

+1

Alvin Nguyen you are expected to print the entire row of the day which has maximum cases. If you pay attention to the lesson and the examples in that module, groupby actually groups the dataset by the column and outputs only the column which is used to group and the info extracted through method used like sum(), max(), whatever. It doesn't print the entire row. 🅰🅹 (Challenge Accepted) sir has given another interesting idea.

+1

I see. I just gotta see and learn how to filter using conditions. That seems to be important

+1

CHANDAN ROY No that solution was for the COVID Data Analysis Project. 😃😃 I saw question again then realised that I am giving solution for that project.

+1

🅰🅹 (Challenge Accepted) Yeah I was about to try your idea but that vanished before I could analyse that. I found that to be different and so thought that could a new way.

+1

@CHANDAN import pandas as pd df = pd.read_csv("/usercode/files/ca-covid.csv") df.drop('state', axis=1, inplace=True) df.set_index('date', inplace=True) df['ratio'] = df['deaths']/df['cases'] worst_day = df [(df['ratio']== df['ratio'].max())] print(worst_day) Your lesson really helped me for the final project. It's great. Not sure why the practice is harder than the project itself. Thank you