df=pd.read_csv('./ipl/all_matches.csv')
df1=df[['match_id','season','venue','innings','striker','bowler','batting_team','bowling_team','ball','runs_off_bat','extras']]
df1=df1.loc[(df1['ball'] < 6.1)]
df1['total'] = df1['runs_off_bat'] + df1['extras']
Asked
Active
Viewed 105 times
1

desertnaut
- 57,590
- 26
- 140
- 166

SOUVIK GHOSH
- 27
- 5
-
you want to group by the match id and sum the runs. have a look at: [pandas groupby and sum](https://stackoverflow.com/q/39922986/13138364) – tdy May 01 '21 at 08:00
-
Does this answer your question? [Pandas group-by and sum](https://stackoverflow.com/questions/39922986/pandas-group-by-and-sum) – bad_coder May 02 '21 at 09:35
1 Answers
0
Few changes that I've made:
- Specify the columns that you wanna read in the pd.read_csv args.
- groupby
match_id
and aggregated the sum of thetotal
column for each group.
use_col = ['match_id','season','venue','innings','striker','bowler','batting_team','bowling_team','ball','runs_off_bat','extras']
df=pd.read_csv('./ipl/all_matches.csv', usecols= use_col)
df1=df1.loc[(df1['ball'] < 6.1)]
df1['total'] = df1['runs_off_bat'] + df1['extras']
total_run_per_match = df1.groupby('match_id').agg({'total': sum})
If you wanna preserve the original data frame:
df1['total_run_per_match'] = df1.groupby('match_id')['transform'].sum()

desertnaut
- 57,590
- 26
- 140
- 166

Nk03
- 14,699
- 2
- 8
- 22
-
But I want to store that total in that dataframe itself.So how can i do that – SOUVIK GHOSH May 01 '21 at 08:29
-
Wrong number of items passed 3, placement implies 1 ----the above code generates this error – SOUVIK GHOSH May 01 '21 at 09:34