+ 1

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

13th Apr 2021, 4:22 PM
Alvin Nguyen
Alvin Nguyen - avatar
9 Respostas
+ 7
You are almost there. Just small change https://code.sololearn.com/c43A21a19A22/?ref=app
13th Apr 2021, 4:31 PM
CHANDAN ROY
CHANDAN ROY - avatar
+ 3
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) searchedMonth = input() table = df[df['month'] == searchedMonth ] table = table[table.groupby('date')['cases'].max() == table['cases'].max()] print(table)
6th Nov 2022, 5:01 PM
Ilyas Errarhoute
Ilyas Errarhoute - avatar
+ 1
wow CHANDAN ROY I really appreciate it man. It has some more confusing syntax
13th Apr 2021, 4:32 PM
Alvin Nguyen
Alvin Nguyen - avatar
+ 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.
13th Apr 2021, 4:40 PM
CHANDAN ROY
CHANDAN ROY - avatar
+ 1
I see. I just gotta see and learn how to filter using conditions. That seems to be important
13th Apr 2021, 4:41 PM
Alvin Nguyen
Alvin Nguyen - avatar
+ 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.
13th Apr 2021, 4:50 PM
AÍąJ
AÍąJ - avatar
+ 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.
13th Apr 2021, 4:52 PM
CHANDAN ROY
CHANDAN ROY - avatar
+ 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
13th Apr 2021, 5:06 PM
Alvin Nguyen
Alvin Nguyen - avatar
0
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_user=input() maxim=df[df["month"]==month_user]['cases'].max() print(df[df['cases']==maxim])
21st Feb 2023, 12:52 PM
CHEPTOO MARION
CHEPTOO MARION - avatar