pandas - Trying to create a new dataframe based on group by calculation on an existing dataframe in Python - Stack Overflow
- c - Solaris 10 make Error code 1 Fatal Error when trying to build python 2.7.16 - Stack Overflow 推荐度:
- javascript - How to dismiss a phonegap notification programmatically - Stack Overflow 推荐度:
- javascript - Get the JSON objects that are not present in another array - Stack Overflow 推荐度:
- javascript - VS 2015 Angular 2 import modules cannot be resolved - Stack Overflow 推荐度:
- javascript - Type 'undefined' is not assignable to type 'menuItemProps[]' - Stack Overflow 推荐度:
- 相关推荐
I have an 18,000 record dataset in the below format:
Date | Tm | Site | Opp | Player | Dist | Made | Blocked | GameID | Season |
---|---|---|---|---|---|---|---|---|---|
2024-01-07 | ARI | H | SEA | Matt Prater | 51 | N | N | SEA @ ARI | 2023 |
2024-01-07 | DAL | A | WAS | Brandon Aubrey | 50 | Y | N | DAL @ WAS | 2023 |
2024-01-07 | TAM | A | CAR | Chase McLaughlin | 57 | Y | N | TAM @ CAR | 2023 |
2024-01-07 | CAR | H | TAM | Matthew Wright | 52 | N | N | TAM @ CAR | 2023 |
2024-01-07 | CHI | A | GNB | Cairo Santos | 50 | Y | N | CHI @ GNB | 2023 |
I have an 18,000 record dataset in the below format:
Date | Tm | Site | Opp | Player | Dist | Made | Blocked | GameID | Season |
---|---|---|---|---|---|---|---|---|---|
2024-01-07 | ARI | H | SEA | Matt Prater | 51 | N | N | SEA @ ARI | 2023 |
2024-01-07 | DAL | A | WAS | Brandon Aubrey | 50 | Y | N | DAL @ WAS | 2023 |
2024-01-07 | TAM | A | CAR | Chase McLaughlin | 57 | Y | N | TAM @ CAR | 2023 |
2024-01-07 | CAR | H | TAM | Matthew Wright | 52 | N | N | TAM @ CAR | 2023 |
2024-01-07 | CHI | A | GNB | Cairo Santos | 50 | Y | N | CHI @ GNB | 2023 |
There is data for 50 seasons. My goal for this part of my project is to calculate the number of attempts (each line is one attempt) per game (unique GameID) by season. My thought was the best route is to create a dataframe that has columns for season, attempts, games, and average per game.
I've run a calculation for attempts by using:
df.groupby(['Season']).size()
And unique games by using:
df.groupby('Season')['GameID'].nunique()
Each of these brings back a table by year, so I was thinking that I could create a dictionary with the three fields to build a new dataframe.
data = {"Year":df.groupby(['Season']), "FG":df.groupby(['Season']).size(), "Games":df.groupby('Season')['GameID'].nunique()}
dfgrp = pd.DataFrame(data)
But I get a very long error when I try to view dfgrp, where it stops iteration but doesn't identify what the issue is.
I've tried looking through multiple searches but there doesn't seem to be a matching question that addresses this issue. Am I going about this the wrong way?
Share Improve this question asked Nov 15, 2024 at 21:39 AbartelAbartel 274 bronze badges 2 |1 Answer
Reset to default 0You could skip a few steps with pd.groupby.agg().
df.groupby('Season').agg(size=('Season', 'size'),
nunique=('GameID', 'nunique'))
size nunique
Season
2023 5 4
- 芯片缺货严重,真凶或是ABF产能不足
- 微信支付和支付宝口水战开打!
- 力压谷歌、苹果,微软凭什么成为软件霸主?
- laravel - Filament | Processing Attachment using API through Email via SendGrid - Stack Overflow
- Formulas in Looker Studio with Hubspot data not working - Stack Overflow
- sqlite - invalid call to function 'connect' in base 'NAtiveScript'. expected 3 arguments in godo
- vuejs3 - Vue 3 Composition API data() - Generic idea required - Stack Overflow
- c - How to properly implement HKDF Expand with openssl EVP_KDF - Stack Overflow
- I want to run simple php with frankenphp inside docker - Stack Overflow
- How to list ALL Zoom cloud recordings? - Stack Overflow
- c++ - C++20 compile time retrieve size of a vector - Stack Overflow
- postgresql - Postgres Postgis ST_DWithin query is not accurate - Stack Overflow
- node.js - node-gyp fails with parsing vs version: undefined - Stack Overflow
- google cloud platform - Java application unable to find ADC when Workload Identity is enabled on GKE cluster - Stack Overflow
- Swift Predicate: Fatal Error Keypaths With Multiple Components - Stack Overflow
- reactjs - WatermelonDB's withObservables leaking when using with Jest - Stack Overflow
- excel - Are there any advantages to use `Application.Match` on a VBA array instead of looping over it when you only care if an e
out = df.groupby(['Season'], as_index=False).agg(FG=('Season', 'size'), Games=('GameID', 'nunique'))
if you add what you expect the output to be, it would be easier to help – iBeMeltin Commented Nov 15, 2024 at 21:55groupby(by=['Season', 'GameID']).nunique()?
From your post I assume that you need information about each GameID in the season, which are lost using.agg()
. – yellow_dot Commented Nov 16, 2024 at 15:33