delete observations with less than X consecutive datesIs there a simple way to delete a list element by value?Delete an element from a dictionaryDelete a file or folderWhy is reading lines from stdin much slower in C++ than Python?Delete column from pandas DataFrame by column namePython Pandas - replace values with NAN in multiple columns based on mutliple dates?Max & Min Date in Python Pandas DataFramePandas dataframe delete rows if the gap is more than 3 daysDelete non-consecutive values from a dataframe columnHow to group the data by the day-month parts of the Date regardless of the year part?

Taxes on Dividends in a Roth IRA

US tourist/student visa

Has any country ever had 2 former presidents in jail simultaneously?

How to convince somebody that he is fit for something else, but not this job?

How to explain what's wrong with this application of the chain rule?

Does the reader need to like the PoV character?

Can I say "fingers" when referring to toes?

Doesn't the system of the Supreme Court oppose justice?

What to do when eye contact makes your coworker uncomfortable?

What are some good ways to treat frozen vegetables such that they behave like fresh vegetables when stir frying them?

"It doesn't matter" or "it won't matter"?

How do I tell my boss that I'm quitting soon, especially given that a colleague just left this week

When were female captains banned from Starfleet?

Shouldn’t conservatives embrace universal basic income?

Why does this expression simplify as such?

Microchip documentation does not label CAN buss pins on micro controller pinout diagram

How would you translate "more" for use as an interface button?

How can I, as DM, avoid the Conga Line of Death occurring when implementing some form of flanking rule?

Can you use Vicious Mockery to win an argument or gain favours?

PTIJ: Why is Haman obsessed with Bose?

A variation to the phrase "hanging over my shoulders"

Why do ¬, ∀ and ∃ have the same precedence?

The Digit Triangles

Does the Linux kernel need a file system to run?



delete observations with less than X consecutive dates


Is there a simple way to delete a list element by value?Delete an element from a dictionaryDelete a file or folderWhy is reading lines from stdin much slower in C++ than Python?Delete column from pandas DataFrame by column namePython Pandas - replace values with NAN in multiple columns based on mutliple dates?Max & Min Date in Python Pandas DataFramePandas dataframe delete rows if the gap is more than 3 daysDelete non-consecutive values from a dataframe columnHow to group the data by the day-month parts of the Date regardless of the year part?













1















the following dataframe which contains data for the same company (column ID) at different dates (column date). I would like to delete the observations for which there are less than 3 days.



The starting dataset is



df = pd.DataFrame("ID":"0":1,"1":1,"2":1,"3":1,"4":4,"5":4,"6":4,"7":2,"8":2,"9":3,"10":3,
"date":"0":1421020800000,"1":1421193600000,"2":1422489600000,"3":1423353600000,"4":1421020800000,"5":1421107200000,"6":1421193600000,"7":1421020800000,"8":1421107200000,"9":1421452800000,"10":1421539200000,
"variable":"0":28,"1":62,"2":60,"3":72,"4":28,"5":61,"6":62,"7":23,"8":70,"9":32,"10":55)
df.date = pd.to_datetime(df.date, unit='ms')
df.sort_values(by=["ID", "date"],inplace=True)


In the above dataframe, only company with ID = 4 satisfies the requirement and I would like to delete the others.



I wrote the following code but it has an obvious problem and I can't figure out how to fix it:



df['delete'] = 0
for name, group in df.groupby(by = "ID"):
if group.shape[0] < 3:
df.loc[df['ID']==name,'delete'] = 1
df = df.loc[df['delete'] == 0,:]


The above code keeps both companies with ID=1 and ID=4; ID=1 should be cancelled because it contains 4 datapoints but at maximum two of them are on consecutive days (while I want to impose a minimum of 3).



Any help would be much appreciated. Thank you










share|improve this question


























    1















    the following dataframe which contains data for the same company (column ID) at different dates (column date). I would like to delete the observations for which there are less than 3 days.



    The starting dataset is



    df = pd.DataFrame("ID":"0":1,"1":1,"2":1,"3":1,"4":4,"5":4,"6":4,"7":2,"8":2,"9":3,"10":3,
    "date":"0":1421020800000,"1":1421193600000,"2":1422489600000,"3":1423353600000,"4":1421020800000,"5":1421107200000,"6":1421193600000,"7":1421020800000,"8":1421107200000,"9":1421452800000,"10":1421539200000,
    "variable":"0":28,"1":62,"2":60,"3":72,"4":28,"5":61,"6":62,"7":23,"8":70,"9":32,"10":55)
    df.date = pd.to_datetime(df.date, unit='ms')
    df.sort_values(by=["ID", "date"],inplace=True)


    In the above dataframe, only company with ID = 4 satisfies the requirement and I would like to delete the others.



    I wrote the following code but it has an obvious problem and I can't figure out how to fix it:



    df['delete'] = 0
    for name, group in df.groupby(by = "ID"):
    if group.shape[0] < 3:
    df.loc[df['ID']==name,'delete'] = 1
    df = df.loc[df['delete'] == 0,:]


    The above code keeps both companies with ID=1 and ID=4; ID=1 should be cancelled because it contains 4 datapoints but at maximum two of them are on consecutive days (while I want to impose a minimum of 3).



    Any help would be much appreciated. Thank you










    share|improve this question
























      1












      1








      1








      the following dataframe which contains data for the same company (column ID) at different dates (column date). I would like to delete the observations for which there are less than 3 days.



      The starting dataset is



      df = pd.DataFrame("ID":"0":1,"1":1,"2":1,"3":1,"4":4,"5":4,"6":4,"7":2,"8":2,"9":3,"10":3,
      "date":"0":1421020800000,"1":1421193600000,"2":1422489600000,"3":1423353600000,"4":1421020800000,"5":1421107200000,"6":1421193600000,"7":1421020800000,"8":1421107200000,"9":1421452800000,"10":1421539200000,
      "variable":"0":28,"1":62,"2":60,"3":72,"4":28,"5":61,"6":62,"7":23,"8":70,"9":32,"10":55)
      df.date = pd.to_datetime(df.date, unit='ms')
      df.sort_values(by=["ID", "date"],inplace=True)


      In the above dataframe, only company with ID = 4 satisfies the requirement and I would like to delete the others.



      I wrote the following code but it has an obvious problem and I can't figure out how to fix it:



      df['delete'] = 0
      for name, group in df.groupby(by = "ID"):
      if group.shape[0] < 3:
      df.loc[df['ID']==name,'delete'] = 1
      df = df.loc[df['delete'] == 0,:]


      The above code keeps both companies with ID=1 and ID=4; ID=1 should be cancelled because it contains 4 datapoints but at maximum two of them are on consecutive days (while I want to impose a minimum of 3).



      Any help would be much appreciated. Thank you










      share|improve this question














      the following dataframe which contains data for the same company (column ID) at different dates (column date). I would like to delete the observations for which there are less than 3 days.



      The starting dataset is



      df = pd.DataFrame("ID":"0":1,"1":1,"2":1,"3":1,"4":4,"5":4,"6":4,"7":2,"8":2,"9":3,"10":3,
      "date":"0":1421020800000,"1":1421193600000,"2":1422489600000,"3":1423353600000,"4":1421020800000,"5":1421107200000,"6":1421193600000,"7":1421020800000,"8":1421107200000,"9":1421452800000,"10":1421539200000,
      "variable":"0":28,"1":62,"2":60,"3":72,"4":28,"5":61,"6":62,"7":23,"8":70,"9":32,"10":55)
      df.date = pd.to_datetime(df.date, unit='ms')
      df.sort_values(by=["ID", "date"],inplace=True)


      In the above dataframe, only company with ID = 4 satisfies the requirement and I would like to delete the others.



      I wrote the following code but it has an obvious problem and I can't figure out how to fix it:



      df['delete'] = 0
      for name, group in df.groupby(by = "ID"):
      if group.shape[0] < 3:
      df.loc[df['ID']==name,'delete'] = 1
      df = df.loc[df['delete'] == 0,:]


      The above code keeps both companies with ID=1 and ID=4; ID=1 should be cancelled because it contains 4 datapoints but at maximum two of them are on consecutive days (while I want to impose a minimum of 3).



      Any help would be much appreciated. Thank you







      python pandas






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 7 at 23:49









      cycler10cycler10

      103




      103






















          3 Answers
          3






          active

          oldest

          votes


















          0














          IIUC using diff + cumsum with date column create the group key New, then we just using groupby + filter the unwanted groups



          df['New']=df.groupby('ID').date.apply(lambda x : x.diff().dt.days.ne(1).cumsum())
          yourdf=df.groupby(['ID','New']).filter(lambda x : len(x)>=3)
          yourdf
          Out[809]:
          ID date variable New
          4 4 2015-01-12 28 1
          5 4 2015-01-13 61 1
          6 4 2015-01-14 62 1





          share|improve this answer






























            0














            I think you could replace the "group.shape[0]" applying a moving window of 3 days and count items.



            df = pd.DataFrame("ID":"0":1,"1":1,"2":1,"3":1,"4":4,"5":4,"6":4,"7":2,"8":2,"9":3,"10":3,
            "date":"0":1421020800000,"1":1421193600000,"2":1422489600000,"3":1423353600000,"4":1421020800000,"5":1421107200000,"6":1421193600000,"7":1421020800000,"8":1421107200000,"9":1421452800000,"10":1421539200000,
            "variable":"0":28,"1":62,"2":60,"3":72,"4":28,"5":61,"6":62,"7":23,"8":70,"9":32,"10":55)
            df.date = pd.to_datetime(df.date, unit='ms')
            df.sort_values(by=["ID", "date"],inplace=True)

            df['delete'] = 0
            for name, group in df.groupby(by = "ID"):
            group.set_index('date',inplace=True)

            if group.rolling(window='3D',min_periods=0).count()['delete'].max() < 3:
            df.loc[df['ID']==name,'delete'] = 1
            df = df.loc[df['delete'] == 0,:]





            share|improve this answer






























              0














              df['delete'] = 0
              for name, group in df.groupby(by = "ID"):
              if group.shape[0] != 3:
              df.loc[df['ID']==name,'delete'] = 1
              df = df.loc[df['delete'] == 0,:]


              you may set wrong in if group.shape[0] != 3






              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%2f55054640%2fdelete-observations-with-less-than-x-consecutive-dates%23new-answer', 'question_page');

                );

                Post as a guest















                Required, but never shown

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                0














                IIUC using diff + cumsum with date column create the group key New, then we just using groupby + filter the unwanted groups



                df['New']=df.groupby('ID').date.apply(lambda x : x.diff().dt.days.ne(1).cumsum())
                yourdf=df.groupby(['ID','New']).filter(lambda x : len(x)>=3)
                yourdf
                Out[809]:
                ID date variable New
                4 4 2015-01-12 28 1
                5 4 2015-01-13 61 1
                6 4 2015-01-14 62 1





                share|improve this answer



























                  0














                  IIUC using diff + cumsum with date column create the group key New, then we just using groupby + filter the unwanted groups



                  df['New']=df.groupby('ID').date.apply(lambda x : x.diff().dt.days.ne(1).cumsum())
                  yourdf=df.groupby(['ID','New']).filter(lambda x : len(x)>=3)
                  yourdf
                  Out[809]:
                  ID date variable New
                  4 4 2015-01-12 28 1
                  5 4 2015-01-13 61 1
                  6 4 2015-01-14 62 1





                  share|improve this answer

























                    0












                    0








                    0







                    IIUC using diff + cumsum with date column create the group key New, then we just using groupby + filter the unwanted groups



                    df['New']=df.groupby('ID').date.apply(lambda x : x.diff().dt.days.ne(1).cumsum())
                    yourdf=df.groupby(['ID','New']).filter(lambda x : len(x)>=3)
                    yourdf
                    Out[809]:
                    ID date variable New
                    4 4 2015-01-12 28 1
                    5 4 2015-01-13 61 1
                    6 4 2015-01-14 62 1





                    share|improve this answer













                    IIUC using diff + cumsum with date column create the group key New, then we just using groupby + filter the unwanted groups



                    df['New']=df.groupby('ID').date.apply(lambda x : x.diff().dt.days.ne(1).cumsum())
                    yourdf=df.groupby(['ID','New']).filter(lambda x : len(x)>=3)
                    yourdf
                    Out[809]:
                    ID date variable New
                    4 4 2015-01-12 28 1
                    5 4 2015-01-13 61 1
                    6 4 2015-01-14 62 1






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Mar 8 at 0:10









                    Wen-BenWen-Ben

                    119k83569




                    119k83569























                        0














                        I think you could replace the "group.shape[0]" applying a moving window of 3 days and count items.



                        df = pd.DataFrame("ID":"0":1,"1":1,"2":1,"3":1,"4":4,"5":4,"6":4,"7":2,"8":2,"9":3,"10":3,
                        "date":"0":1421020800000,"1":1421193600000,"2":1422489600000,"3":1423353600000,"4":1421020800000,"5":1421107200000,"6":1421193600000,"7":1421020800000,"8":1421107200000,"9":1421452800000,"10":1421539200000,
                        "variable":"0":28,"1":62,"2":60,"3":72,"4":28,"5":61,"6":62,"7":23,"8":70,"9":32,"10":55)
                        df.date = pd.to_datetime(df.date, unit='ms')
                        df.sort_values(by=["ID", "date"],inplace=True)

                        df['delete'] = 0
                        for name, group in df.groupby(by = "ID"):
                        group.set_index('date',inplace=True)

                        if group.rolling(window='3D',min_periods=0).count()['delete'].max() < 3:
                        df.loc[df['ID']==name,'delete'] = 1
                        df = df.loc[df['delete'] == 0,:]





                        share|improve this answer



























                          0














                          I think you could replace the "group.shape[0]" applying a moving window of 3 days and count items.



                          df = pd.DataFrame("ID":"0":1,"1":1,"2":1,"3":1,"4":4,"5":4,"6":4,"7":2,"8":2,"9":3,"10":3,
                          "date":"0":1421020800000,"1":1421193600000,"2":1422489600000,"3":1423353600000,"4":1421020800000,"5":1421107200000,"6":1421193600000,"7":1421020800000,"8":1421107200000,"9":1421452800000,"10":1421539200000,
                          "variable":"0":28,"1":62,"2":60,"3":72,"4":28,"5":61,"6":62,"7":23,"8":70,"9":32,"10":55)
                          df.date = pd.to_datetime(df.date, unit='ms')
                          df.sort_values(by=["ID", "date"],inplace=True)

                          df['delete'] = 0
                          for name, group in df.groupby(by = "ID"):
                          group.set_index('date',inplace=True)

                          if group.rolling(window='3D',min_periods=0).count()['delete'].max() < 3:
                          df.loc[df['ID']==name,'delete'] = 1
                          df = df.loc[df['delete'] == 0,:]





                          share|improve this answer

























                            0












                            0








                            0







                            I think you could replace the "group.shape[0]" applying a moving window of 3 days and count items.



                            df = pd.DataFrame("ID":"0":1,"1":1,"2":1,"3":1,"4":4,"5":4,"6":4,"7":2,"8":2,"9":3,"10":3,
                            "date":"0":1421020800000,"1":1421193600000,"2":1422489600000,"3":1423353600000,"4":1421020800000,"5":1421107200000,"6":1421193600000,"7":1421020800000,"8":1421107200000,"9":1421452800000,"10":1421539200000,
                            "variable":"0":28,"1":62,"2":60,"3":72,"4":28,"5":61,"6":62,"7":23,"8":70,"9":32,"10":55)
                            df.date = pd.to_datetime(df.date, unit='ms')
                            df.sort_values(by=["ID", "date"],inplace=True)

                            df['delete'] = 0
                            for name, group in df.groupby(by = "ID"):
                            group.set_index('date',inplace=True)

                            if group.rolling(window='3D',min_periods=0).count()['delete'].max() < 3:
                            df.loc[df['ID']==name,'delete'] = 1
                            df = df.loc[df['delete'] == 0,:]





                            share|improve this answer













                            I think you could replace the "group.shape[0]" applying a moving window of 3 days and count items.



                            df = pd.DataFrame("ID":"0":1,"1":1,"2":1,"3":1,"4":4,"5":4,"6":4,"7":2,"8":2,"9":3,"10":3,
                            "date":"0":1421020800000,"1":1421193600000,"2":1422489600000,"3":1423353600000,"4":1421020800000,"5":1421107200000,"6":1421193600000,"7":1421020800000,"8":1421107200000,"9":1421452800000,"10":1421539200000,
                            "variable":"0":28,"1":62,"2":60,"3":72,"4":28,"5":61,"6":62,"7":23,"8":70,"9":32,"10":55)
                            df.date = pd.to_datetime(df.date, unit='ms')
                            df.sort_values(by=["ID", "date"],inplace=True)

                            df['delete'] = 0
                            for name, group in df.groupby(by = "ID"):
                            group.set_index('date',inplace=True)

                            if group.rolling(window='3D',min_periods=0).count()['delete'].max() < 3:
                            df.loc[df['ID']==name,'delete'] = 1
                            df = df.loc[df['delete'] == 0,:]






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Mar 8 at 6:44









                            Giuseppe SalernoGiuseppe Salerno

                            354




                            354





















                                0














                                df['delete'] = 0
                                for name, group in df.groupby(by = "ID"):
                                if group.shape[0] != 3:
                                df.loc[df['ID']==name,'delete'] = 1
                                df = df.loc[df['delete'] == 0,:]


                                you may set wrong in if group.shape[0] != 3






                                share|improve this answer



























                                  0














                                  df['delete'] = 0
                                  for name, group in df.groupby(by = "ID"):
                                  if group.shape[0] != 3:
                                  df.loc[df['ID']==name,'delete'] = 1
                                  df = df.loc[df['delete'] == 0,:]


                                  you may set wrong in if group.shape[0] != 3






                                  share|improve this answer

























                                    0












                                    0








                                    0







                                    df['delete'] = 0
                                    for name, group in df.groupby(by = "ID"):
                                    if group.shape[0] != 3:
                                    df.loc[df['ID']==name,'delete'] = 1
                                    df = df.loc[df['delete'] == 0,:]


                                    you may set wrong in if group.shape[0] != 3






                                    share|improve this answer













                                    df['delete'] = 0
                                    for name, group in df.groupby(by = "ID"):
                                    if group.shape[0] != 3:
                                    df.loc[df['ID']==name,'delete'] = 1
                                    df = df.loc[df['delete'] == 0,:]


                                    you may set wrong in if group.shape[0] != 3







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Mar 8 at 6:59









                                    Tom.chen.kangTom.chen.kang

                                    265




                                    265



























                                        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%2f55054640%2fdelete-observations-with-less-than-x-consecutive-dates%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







                                        Popular posts from this blog

                                        Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

                                        Posting a File and Associated Data to a RESTful WebService preferably as JSON2019 Community Moderator ElectionREST URI convention - Singular or plural name of resource while creating itUploading both data and files in one form using Ajax?How do I upload a file with metadata using a REST web service?REST API - file (ie images) processing - best practicesSpring: JSON data and file in the same requestREST API Put large dataPosting a file and JSON data to Spring rest serviceRESTful Web Service Upload/Download Large Data With JSONREST API Design sending JSON data and a file to the api in same requestPost JSONArray to REST servicePUT vs. POST in RESTParsing values from a JSON file?JavaScript/jQuery to download file via POST with JSON dataHow do I upload a file with metadata using a REST web service?How to POST JSON data with Curl from Terminal/Commandline to Test Spring REST?Separate REST JSON API server and client?what's the correct way to send a file from REST web service to client?How do I write JSON data to a file?What is “406-Not Acceptable Response” in HTTP?REST API - file (ie images) processing - best practices

                                        How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page