“Python” Get particular rows of an hour from data frame












0















I want to get all rows of each hour or day do some calculations for them.
So i mean how to iterate over data frame and filter rows of an hour some, do calculations and then move to the next hour



Question: How to get the rows of each hour/or day?



df
Date TimeStamp col1
20150102 20:00:00 SomeData
20150102 20:01:00 SomeData
20150102 20:02:00 SomeData
20150102 20:03:00 SomeData
20150102 20:04:00 SomeData
20150102 20:05:00 SomeData


for index, row in df.iterrows():
#grouping these rows of a min/an hour and do some calculations based on these rows









share|improve this question

























  • Can you be more specific about your expected output? It's possible that you can achieve your goal with vectorized operations, which would be way faster and more readable than explicit looping.

    – Peter Leimbigler
    Nov 21 '18 at 1:01











  • I expect to get the rows of each hour (e.g. 20:00:00-20:59:00). I used to do such stuff with iteration but any other way is also accepted.

    – Route
    Nov 21 '18 at 1:08











  • what "stuff" specifically are you intending to do? What calculated value(s) should accompany the rows of each hour?

    – Peter Leimbigler
    Nov 21 '18 at 1:29











  • I will go column by column and use the values in each row of this column to compare many thing such as Momentum, Rate of change, P/L, etc.

    – Route
    Nov 21 '18 at 1:40











  • without concrete example data and a specific description of what you intend to do with it, all I can offer is to echo the posted answer, which is as vague as the question: look into df.groupby. Also check out how to create a minimal, complete, verifiable example: stackoverflow.com/help/mcve

    – Peter Leimbigler
    Nov 21 '18 at 1:46
















0















I want to get all rows of each hour or day do some calculations for them.
So i mean how to iterate over data frame and filter rows of an hour some, do calculations and then move to the next hour



Question: How to get the rows of each hour/or day?



df
Date TimeStamp col1
20150102 20:00:00 SomeData
20150102 20:01:00 SomeData
20150102 20:02:00 SomeData
20150102 20:03:00 SomeData
20150102 20:04:00 SomeData
20150102 20:05:00 SomeData


for index, row in df.iterrows():
#grouping these rows of a min/an hour and do some calculations based on these rows









share|improve this question

























  • Can you be more specific about your expected output? It's possible that you can achieve your goal with vectorized operations, which would be way faster and more readable than explicit looping.

    – Peter Leimbigler
    Nov 21 '18 at 1:01











  • I expect to get the rows of each hour (e.g. 20:00:00-20:59:00). I used to do such stuff with iteration but any other way is also accepted.

    – Route
    Nov 21 '18 at 1:08











  • what "stuff" specifically are you intending to do? What calculated value(s) should accompany the rows of each hour?

    – Peter Leimbigler
    Nov 21 '18 at 1:29











  • I will go column by column and use the values in each row of this column to compare many thing such as Momentum, Rate of change, P/L, etc.

    – Route
    Nov 21 '18 at 1:40











  • without concrete example data and a specific description of what you intend to do with it, all I can offer is to echo the posted answer, which is as vague as the question: look into df.groupby. Also check out how to create a minimal, complete, verifiable example: stackoverflow.com/help/mcve

    – Peter Leimbigler
    Nov 21 '18 at 1:46














0












0








0








I want to get all rows of each hour or day do some calculations for them.
So i mean how to iterate over data frame and filter rows of an hour some, do calculations and then move to the next hour



Question: How to get the rows of each hour/or day?



df
Date TimeStamp col1
20150102 20:00:00 SomeData
20150102 20:01:00 SomeData
20150102 20:02:00 SomeData
20150102 20:03:00 SomeData
20150102 20:04:00 SomeData
20150102 20:05:00 SomeData


for index, row in df.iterrows():
#grouping these rows of a min/an hour and do some calculations based on these rows









share|improve this question
















I want to get all rows of each hour or day do some calculations for them.
So i mean how to iterate over data frame and filter rows of an hour some, do calculations and then move to the next hour



Question: How to get the rows of each hour/or day?



df
Date TimeStamp col1
20150102 20:00:00 SomeData
20150102 20:01:00 SomeData
20150102 20:02:00 SomeData
20150102 20:03:00 SomeData
20150102 20:04:00 SomeData
20150102 20:05:00 SomeData


for index, row in df.iterrows():
#grouping these rows of a min/an hour and do some calculations based on these rows






python python-3.x pandas dataframe






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 1:01







Route

















asked Nov 21 '18 at 1:00









RouteRoute

61




61













  • Can you be more specific about your expected output? It's possible that you can achieve your goal with vectorized operations, which would be way faster and more readable than explicit looping.

    – Peter Leimbigler
    Nov 21 '18 at 1:01











  • I expect to get the rows of each hour (e.g. 20:00:00-20:59:00). I used to do such stuff with iteration but any other way is also accepted.

    – Route
    Nov 21 '18 at 1:08











  • what "stuff" specifically are you intending to do? What calculated value(s) should accompany the rows of each hour?

    – Peter Leimbigler
    Nov 21 '18 at 1:29











  • I will go column by column and use the values in each row of this column to compare many thing such as Momentum, Rate of change, P/L, etc.

    – Route
    Nov 21 '18 at 1:40











  • without concrete example data and a specific description of what you intend to do with it, all I can offer is to echo the posted answer, which is as vague as the question: look into df.groupby. Also check out how to create a minimal, complete, verifiable example: stackoverflow.com/help/mcve

    – Peter Leimbigler
    Nov 21 '18 at 1:46



















  • Can you be more specific about your expected output? It's possible that you can achieve your goal with vectorized operations, which would be way faster and more readable than explicit looping.

    – Peter Leimbigler
    Nov 21 '18 at 1:01











  • I expect to get the rows of each hour (e.g. 20:00:00-20:59:00). I used to do such stuff with iteration but any other way is also accepted.

    – Route
    Nov 21 '18 at 1:08











  • what "stuff" specifically are you intending to do? What calculated value(s) should accompany the rows of each hour?

    – Peter Leimbigler
    Nov 21 '18 at 1:29











  • I will go column by column and use the values in each row of this column to compare many thing such as Momentum, Rate of change, P/L, etc.

    – Route
    Nov 21 '18 at 1:40











  • without concrete example data and a specific description of what you intend to do with it, all I can offer is to echo the posted answer, which is as vague as the question: look into df.groupby. Also check out how to create a minimal, complete, verifiable example: stackoverflow.com/help/mcve

    – Peter Leimbigler
    Nov 21 '18 at 1:46

















Can you be more specific about your expected output? It's possible that you can achieve your goal with vectorized operations, which would be way faster and more readable than explicit looping.

– Peter Leimbigler
Nov 21 '18 at 1:01





Can you be more specific about your expected output? It's possible that you can achieve your goal with vectorized operations, which would be way faster and more readable than explicit looping.

– Peter Leimbigler
Nov 21 '18 at 1:01













I expect to get the rows of each hour (e.g. 20:00:00-20:59:00). I used to do such stuff with iteration but any other way is also accepted.

– Route
Nov 21 '18 at 1:08





I expect to get the rows of each hour (e.g. 20:00:00-20:59:00). I used to do such stuff with iteration but any other way is also accepted.

– Route
Nov 21 '18 at 1:08













what "stuff" specifically are you intending to do? What calculated value(s) should accompany the rows of each hour?

– Peter Leimbigler
Nov 21 '18 at 1:29





what "stuff" specifically are you intending to do? What calculated value(s) should accompany the rows of each hour?

– Peter Leimbigler
Nov 21 '18 at 1:29













I will go column by column and use the values in each row of this column to compare many thing such as Momentum, Rate of change, P/L, etc.

– Route
Nov 21 '18 at 1:40





I will go column by column and use the values in each row of this column to compare many thing such as Momentum, Rate of change, P/L, etc.

– Route
Nov 21 '18 at 1:40













without concrete example data and a specific description of what you intend to do with it, all I can offer is to echo the posted answer, which is as vague as the question: look into df.groupby. Also check out how to create a minimal, complete, verifiable example: stackoverflow.com/help/mcve

– Peter Leimbigler
Nov 21 '18 at 1:46





without concrete example data and a specific description of what you intend to do with it, all I can offer is to echo the posted answer, which is as vague as the question: look into df.groupby. Also check out how to create a minimal, complete, verifiable example: stackoverflow.com/help/mcve

– Peter Leimbigler
Nov 21 '18 at 1:46












1 Answer
1






active

oldest

votes


















0














You should use pandas groupby to group the rows by hour/day then you can perform calculations on the other columns.



Extracting the hours/days into another column would look something like:



df['Hour'] = df['TimeStamp'].dt.hour
df['Day'] = df['Date'].dt.day


After which you would groupby with:



df.groupby('Hour')...
df.groupby('Day')...





share|improve this answer























    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%2f53403858%2fpython-get-particular-rows-of-an-hour-from-data-frame%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 should use pandas groupby to group the rows by hour/day then you can perform calculations on the other columns.



    Extracting the hours/days into another column would look something like:



    df['Hour'] = df['TimeStamp'].dt.hour
    df['Day'] = df['Date'].dt.day


    After which you would groupby with:



    df.groupby('Hour')...
    df.groupby('Day')...





    share|improve this answer




























      0














      You should use pandas groupby to group the rows by hour/day then you can perform calculations on the other columns.



      Extracting the hours/days into another column would look something like:



      df['Hour'] = df['TimeStamp'].dt.hour
      df['Day'] = df['Date'].dt.day


      After which you would groupby with:



      df.groupby('Hour')...
      df.groupby('Day')...





      share|improve this answer


























        0












        0








        0







        You should use pandas groupby to group the rows by hour/day then you can perform calculations on the other columns.



        Extracting the hours/days into another column would look something like:



        df['Hour'] = df['TimeStamp'].dt.hour
        df['Day'] = df['Date'].dt.day


        After which you would groupby with:



        df.groupby('Hour')...
        df.groupby('Day')...





        share|improve this answer













        You should use pandas groupby to group the rows by hour/day then you can perform calculations on the other columns.



        Extracting the hours/days into another column would look something like:



        df['Hour'] = df['TimeStamp'].dt.hour
        df['Day'] = df['Date'].dt.day


        After which you would groupby with:



        df.groupby('Hour')...
        df.groupby('Day')...






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 1:14









        zrelovazrelova

        7181522




        7181522
































            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%2f53403858%2fpython-get-particular-rows-of-an-hour-from-data-frame%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







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini