Getting the Average Value for each Group of a pandas Dataframe










0















I successfully managed to scrape futbin.com for time series price data of Fifa 19 players. I have now got over 200'000 rows with player and price data. For each player I have about 17 different prices (with a respective timestamp).



I would now like to make a new dataframe with only one row per player and the price should be the average price over time for this specific player. Each player has got a unique "Futbin_ID" number.
Until now I was unable to figure out how to do this...
I would really appreciate it if someone could help me out...










share|improve this question

















  • 2





    Have you tried groupby? df_ts.groupby('Futbin_ID')['price'].mean() ?

    – Vishnu Kunchur
    Nov 13 '18 at 19:15












  • This surely must be a duplicate...

    – smj
    Nov 13 '18 at 19:44











  • Possible duplicate of Python Pandas : group by in group by and average?

    – smj
    Nov 13 '18 at 19:49















0















I successfully managed to scrape futbin.com for time series price data of Fifa 19 players. I have now got over 200'000 rows with player and price data. For each player I have about 17 different prices (with a respective timestamp).



I would now like to make a new dataframe with only one row per player and the price should be the average price over time for this specific player. Each player has got a unique "Futbin_ID" number.
Until now I was unable to figure out how to do this...
I would really appreciate it if someone could help me out...










share|improve this question

















  • 2





    Have you tried groupby? df_ts.groupby('Futbin_ID')['price'].mean() ?

    – Vishnu Kunchur
    Nov 13 '18 at 19:15












  • This surely must be a duplicate...

    – smj
    Nov 13 '18 at 19:44











  • Possible duplicate of Python Pandas : group by in group by and average?

    – smj
    Nov 13 '18 at 19:49













0












0








0








I successfully managed to scrape futbin.com for time series price data of Fifa 19 players. I have now got over 200'000 rows with player and price data. For each player I have about 17 different prices (with a respective timestamp).



I would now like to make a new dataframe with only one row per player and the price should be the average price over time for this specific player. Each player has got a unique "Futbin_ID" number.
Until now I was unable to figure out how to do this...
I would really appreciate it if someone could help me out...










share|improve this question














I successfully managed to scrape futbin.com for time series price data of Fifa 19 players. I have now got over 200'000 rows with player and price data. For each player I have about 17 different prices (with a respective timestamp).



I would now like to make a new dataframe with only one row per player and the price should be the average price over time for this specific player. Each player has got a unique "Futbin_ID" number.
Until now I was unable to figure out how to do this...
I would really appreciate it if someone could help me out...







python pandas time-series pandas-groupby






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 19:11









MarcusMarcus

254




254







  • 2





    Have you tried groupby? df_ts.groupby('Futbin_ID')['price'].mean() ?

    – Vishnu Kunchur
    Nov 13 '18 at 19:15












  • This surely must be a duplicate...

    – smj
    Nov 13 '18 at 19:44











  • Possible duplicate of Python Pandas : group by in group by and average?

    – smj
    Nov 13 '18 at 19:49












  • 2





    Have you tried groupby? df_ts.groupby('Futbin_ID')['price'].mean() ?

    – Vishnu Kunchur
    Nov 13 '18 at 19:15












  • This surely must be a duplicate...

    – smj
    Nov 13 '18 at 19:44











  • Possible duplicate of Python Pandas : group by in group by and average?

    – smj
    Nov 13 '18 at 19:49







2




2





Have you tried groupby? df_ts.groupby('Futbin_ID')['price'].mean() ?

– Vishnu Kunchur
Nov 13 '18 at 19:15






Have you tried groupby? df_ts.groupby('Futbin_ID')['price'].mean() ?

– Vishnu Kunchur
Nov 13 '18 at 19:15














This surely must be a duplicate...

– smj
Nov 13 '18 at 19:44





This surely must be a duplicate...

– smj
Nov 13 '18 at 19:44













Possible duplicate of Python Pandas : group by in group by and average?

– smj
Nov 13 '18 at 19:49





Possible duplicate of Python Pandas : group by in group by and average?

– smj
Nov 13 '18 at 19:49












1 Answer
1






active

oldest

votes


















0














You would want to group it by Fubin_ID and then find the mean of each grouping:



avg_price = df_ts.groupby('Futbin_ID')['price'].agg(np.mean)


If you want to have your dataframe with the other columns as well, you can drop the duplicates in the original except the first and replace the price value with the average:



df_ts.drop_duplicates(subset="Futbin_ID", keep="first", inplace= True)
df_ts.join[avg_price.set_index("Fubin_ID"), on="Futbin_ID"]


you can read more about groupby here: https://www.tutorialspoint.com/python_pandas/python_pandas_groupby.htm






share|improve this answer

























  • Thank you very much! But when I do this I get a new df with only two columns (price and Futbin_ID). How can I retain all columns?

    – Marcus
    Nov 13 '18 at 19:46











  • @Marcus You need to define what method to use to aggregate values for the other columns. Since it looks like they are basically just duplicated 'first' is what you want

    – ALollz
    Nov 13 '18 at 20:26










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287976%2fgetting-the-average-value-for-each-group-of-a-pandas-dataframe%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You would want to group it by Fubin_ID and then find the mean of each grouping:



avg_price = df_ts.groupby('Futbin_ID')['price'].agg(np.mean)


If you want to have your dataframe with the other columns as well, you can drop the duplicates in the original except the first and replace the price value with the average:



df_ts.drop_duplicates(subset="Futbin_ID", keep="first", inplace= True)
df_ts.join[avg_price.set_index("Fubin_ID"), on="Futbin_ID"]


you can read more about groupby here: https://www.tutorialspoint.com/python_pandas/python_pandas_groupby.htm






share|improve this answer

























  • Thank you very much! But when I do this I get a new df with only two columns (price and Futbin_ID). How can I retain all columns?

    – Marcus
    Nov 13 '18 at 19:46











  • @Marcus You need to define what method to use to aggregate values for the other columns. Since it looks like they are basically just duplicated 'first' is what you want

    – ALollz
    Nov 13 '18 at 20:26















0














You would want to group it by Fubin_ID and then find the mean of each grouping:



avg_price = df_ts.groupby('Futbin_ID')['price'].agg(np.mean)


If you want to have your dataframe with the other columns as well, you can drop the duplicates in the original except the first and replace the price value with the average:



df_ts.drop_duplicates(subset="Futbin_ID", keep="first", inplace= True)
df_ts.join[avg_price.set_index("Fubin_ID"), on="Futbin_ID"]


you can read more about groupby here: https://www.tutorialspoint.com/python_pandas/python_pandas_groupby.htm






share|improve this answer

























  • Thank you very much! But when I do this I get a new df with only two columns (price and Futbin_ID). How can I retain all columns?

    – Marcus
    Nov 13 '18 at 19:46











  • @Marcus You need to define what method to use to aggregate values for the other columns. Since it looks like they are basically just duplicated 'first' is what you want

    – ALollz
    Nov 13 '18 at 20:26













0












0








0







You would want to group it by Fubin_ID and then find the mean of each grouping:



avg_price = df_ts.groupby('Futbin_ID')['price'].agg(np.mean)


If you want to have your dataframe with the other columns as well, you can drop the duplicates in the original except the first and replace the price value with the average:



df_ts.drop_duplicates(subset="Futbin_ID", keep="first", inplace= True)
df_ts.join[avg_price.set_index("Fubin_ID"), on="Futbin_ID"]


you can read more about groupby here: https://www.tutorialspoint.com/python_pandas/python_pandas_groupby.htm






share|improve this answer















You would want to group it by Fubin_ID and then find the mean of each grouping:



avg_price = df_ts.groupby('Futbin_ID')['price'].agg(np.mean)


If you want to have your dataframe with the other columns as well, you can drop the duplicates in the original except the first and replace the price value with the average:



df_ts.drop_duplicates(subset="Futbin_ID", keep="first", inplace= True)
df_ts.join[avg_price.set_index("Fubin_ID"), on="Futbin_ID"]


you can read more about groupby here: https://www.tutorialspoint.com/python_pandas/python_pandas_groupby.htm







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 22:22

























answered Nov 13 '18 at 19:28









Turtalicious Turtalicious

524




524












  • Thank you very much! But when I do this I get a new df with only two columns (price and Futbin_ID). How can I retain all columns?

    – Marcus
    Nov 13 '18 at 19:46











  • @Marcus You need to define what method to use to aggregate values for the other columns. Since it looks like they are basically just duplicated 'first' is what you want

    – ALollz
    Nov 13 '18 at 20:26

















  • Thank you very much! But when I do this I get a new df with only two columns (price and Futbin_ID). How can I retain all columns?

    – Marcus
    Nov 13 '18 at 19:46











  • @Marcus You need to define what method to use to aggregate values for the other columns. Since it looks like they are basically just duplicated 'first' is what you want

    – ALollz
    Nov 13 '18 at 20:26
















Thank you very much! But when I do this I get a new df with only two columns (price and Futbin_ID). How can I retain all columns?

– Marcus
Nov 13 '18 at 19:46





Thank you very much! But when I do this I get a new df with only two columns (price and Futbin_ID). How can I retain all columns?

– Marcus
Nov 13 '18 at 19:46













@Marcus You need to define what method to use to aggregate values for the other columns. Since it looks like they are basically just duplicated 'first' is what you want

– ALollz
Nov 13 '18 at 20:26





@Marcus You need to define what method to use to aggregate values for the other columns. Since it looks like they are basically just duplicated 'first' is what you want

– ALollz
Nov 13 '18 at 20:26

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287976%2fgetting-the-average-value-for-each-group-of-a-pandas-dataframe%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3