-1

My problem is the following, I want to sum up all values of the column new_results_reported, which have the Overall_Outcome 'Positive' and belong to the state Alabama.

Filter out all columns that have the state_name Alabama, have an overall_outcome 'Positive' and display the sum of them in the column new_results_reported.

state_name                object
overall_outcome           object
date                      object
new_results_reported       int64
total_results_reported     int64
dtype: object

output of new_results_reported from state Alabama should be in Integer

I did some research and came up with something like:

for overall_outcome in table1:
    if overall_outcome != 'Positive':

Unfortunately, I have absolutely no idea how to set up functions or how to do it best.

I would appreciate your help very much.

table view with colums

Prakash Dahal
  • 4,388
  • 2
  • 11
  • 25
Work_fl0w
  • 21
  • 4

2 Answers2

0

Based on the hints that you have given in your question,the following code will do the work. Please research on some builtin python function like zip which will zip the elements of same index of many lists.

I assume that your dataframe name is table1

total_sum = 0

for state_name, overall_outcome, dt, new_results_reported in zip(table1['state_name'].values.tolist(), table1['overall_outcome'].values.tolist(),table1['date'].values.tolist(), table1['new_results_reported'].values.tolist()):
  ndt = int(str(dt)[:4])
  if (state_name == 'Alabama') and (overall_outcome == 'Positive') and (ndt == 2020):
    total_sum += int(new_results_reported)

print(total_sum)

Prakash Dahal
  • 4,388
  • 2
  • 11
  • 25
0

i can roughly understand the codes. i just never understand how you get the codes. I have another problem. I want to compare the case numbers of alabama 2020 now with case numbers of another state in a plot.

: for overall_outcome in table1:
    if overall_outcome != 'Positive':
        plt.plot(table1.date[1:10], table1.new_results_reported[1:10])
        plt.figure(figsize=(8,5))

everything is totally confusing, because it throws all the data in.

how can I display here only the data of Alabama 2020 and anothe state in comparison?

Work_fl0w
  • 21
  • 4
  • Are your data in a pandas dataframe ? if it is the case it should be easy to filter it. – Malo Jun 02 '21 at 19:22
  • yes my data is in a pandas df, but how can i filter it? i want the total amount of new_results_reported from each state in a new table – Work_fl0w Jun 02 '21 at 19:32
  • As you already accepted an answer, you should create a new different question, and show code of your pandas dataframe ... with column names and sample data. Something like df[df['state_name']=='Alabama' & pd.DatetimeIndex(df['date']).year =='2020']. This is duplicate question of : https://stackoverflow.com/questions/48978550/pandas-filtering-multiple-conditions – Malo Jun 02 '21 at 19:38
  • i already opened a new question.. can u have a look? ans see if u can help me with that please – Work_fl0w Jun 02 '21 at 19:39