-2

I have data frame as photo and I did for loop as

enter image description here

for n in datafram[datafram['logic'] == 1].groupby((datafram['logic'] != 1).cumsum()):
    
    print(n)

and the result:

(68,             points=>90%  points<90%  logic
time                                      
2000-07-08    57.246744   42.753256      1
2000-07-09    52.494504   47.505496      1)
(73,             points=>90%  points<90%  logic
time                                      
2000-07-15    52.545239   47.454761      1
2000-07-16    50.093015   49.906985      1
2000-07-17    50.465077   49.534923      1)
(86,             points=>90%  points<90%  logic
time                                      
2000-07-31    53.847455   46.152545      1
2000-08-01    56.434974   43.565026      1
2000-08-02    56.942330   43.057670      1)

how do this result as pandas data frame as:

time          points=>90%  points<90%  logic 
                         
2000-07-08    57.246744   42.753256      1
2000-07-09    52.494504   47.505496      1

2000-07-15    52.545239   47.454761      1
2000-07-16    50.093015   49.906985      1
2000-07-17    50.465077   49.534923      1)

2000-07-31    53.847455   46.152545      1
2000-08-01    56.434974   43.565026      1
2000-08-02    56.942330   43.057670      1
Michael Szczesny
  • 4,911
  • 5
  • 15
  • 32
  • Please see how to create a minimum reproducible example here: https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples Kindly: 1) Format your code in your StackOverflow question, provide sample input data (no links and no images) as well as expected output (no links and no images). – David Erickson Oct 21 '20 at 21:43
  • 1
    @G.Anderson - There is no aggregation. It's grouped by the inverse of the selected rows and `cumsum()`. I really don't know how this is working at all. – Michael Szczesny Oct 21 '20 at 21:54
  • @hasanainkhalil - Your expected output is equal to `datafram[datafram['logic'] == 1]`. I don't know how your grouping even could work, but in the end you just get rid of the groups. – Michael Szczesny Oct 21 '20 at 22:06
  • well, my expected output should be grouped by days so when i use datafram[datafram['logic'] == 1] it will be continues list . – hasanain khalil Oct 22 '20 at 09:24

2 Answers2

0

Yes of course. You can create an empty list before loop. Add all the data in list that you want to add in dataframe. Then you can add list data to dataframe.

Assuming you have list L then,

df = pandas.DataFrame([L])
df.columns = ['col1','col2','col3'.....]  // Adding first row containg column names

Here you have your dataframe. Cheers!

Michael Szczesny
  • 4,911
  • 5
  • 15
  • 32
0

It's hard to say but my guess would be (assuming you have imported pandas as pd):

grouping = datafram[datafram['logic'] == 1].groupby((datafram['logic'] != 1).cumsum())
df = pd.concat(frame for _, frame in grouping)

Since you haven't aggregated in the groupby you have to collect the frames out of the groupings, which are tuples of the form (int, pd.DataFrame).

Timus
  • 10,974
  • 5
  • 14
  • 28