Reputation: 271
I have a df as shown below:
DateTime ModFlow(cfs) ObsFlow(cfs) ModVol(f3) ObsVol(f3)
Event
Event 1 8/15/2016 15:35 11.85926 0 0.039530867 0
Event 1 8/15/2016 10:05 30.05923 0 0.100197433 0
Event 1 8/15/2016 10:00 31.10118 0 0.1036706 0
Event 1 8/15/2016 9:55 32.17444 0 0.107248133 0
Event 1 8/15/2016 4:10 0.6783166 0.5650155 0.002261055 0.001883385
Event 10 6/23/2016 4:35 0.5573569 0.4814242 0.001857856 0.001604747
Event 10 6/23/2016 4:40 0.5536903 0.3544892 0.001845634 0.001181631
Event 10 6/23/2016 4:45 0.5502114 0.368421 0.001834038 0.00122807
Event 10 6/23/2016 4:50 0.5698021 0.501548 0.00189934 0.001671827
Event 10 6/23/2016 4:55 0.7525368 0.879257 0.002508456 0.002930857
Event 11 6/10/2016 8:05 0.6593155 0.6145511 0.002197718 0.002048504
Event 11 6/10/2016 8:10 0.6621117 0.8405573 0.002207039 0.002801858
Event 11 6/10/2016 8:15 0.6578091 0.8173375 0.002192697 0.002724458
Event 11 6/10/2016 8:20 0.6581948 0.871517 0.002193983 0.002905057
Event 12 4/26/2016 22:00 2.307288 2.588235 0.00769096 0.00862745
Event 12 4/26/2016 22:05 2.366998 3.091331 0.007889993 0.010304437
Event 12 4/26/2016 22:10 2.494073 3.278638 0.008313577 0.010928793
Event 12 4/26/2016 22:15 2.746868 3.083591 0.009156227 0.010278637
Event 12 4/26/2016 22:20 3.146326 2.877709 0.010487753 0.009592363
Event 12 4/26/2016 22:30 4.090476 2.354489 0.01363492 0.007848297
Similarly upto Event 47
I want to have a seperate dataframe for each event. Expected output would look like:
Event1_df:
DateTime ModFlow(cfs) ObsFlow(cfs) ModVol(f3) Obs Vol(f3)
Event
8/15/2016 15:35 11.85926 0 0.039530867 0
8/15/2016 10:05 30.05923 0 0.100197433 0
Event 1 8/15/2016 10:00 31.10118 0 0.1036706 0
8/15/2016 9:55 32.17444 0 0.107248133 0
8/15/2016 4:10 0.6783166 0.5650155 0.002261055 0.001883385
Event2_df:
DateTime ModFlow(cfs) ObsFlow(cfs) ModVol(f3) Obs Vol(f3)
Event
8/15/2016 15:35 11.85926 0 0.039530867 0
8/15/2016 10:05 30.05923 0 0.100197433 0
Event 2 8/15/2016 10:00 31.10118 0 0.1036706 0
8/15/2016 9:55 32.17444 0 0.107248133 0
8/15/2016 4:10 0.6783166 0.5650155 0.002261055 0.001883385
Similarly for other events
How could i possibly do it?
Upvotes: 1
Views: 514
Reputation: 36623
You won't be able to assign them to variable names without hard-coding in those names. You can, however, create a dictionary that stores a key to each dataframe. In this case a dictionary comprehension:
> event_dict = {event: df.loc[event] for event in df.index.unique()}
> event_dict['Event1']
# returns
DateTime ModFlow(cfs) ObsFlow(cfs) ModVol(f3) ObsVol(f3)
Event
Event1 8/15/2016 11.859260 0.000000 0.039531 0.000000
Event1 8/15/2016 30.059230 0.000000 0.100197 0.000000
Event1 8/15/2016 31.101180 0.000000 0.103671 0.000000
Event1 8/15/2016 32.174440 0.000000 0.107248 0.000000
Event1 8/15/2016 0.678317 0.565016 0.002261 0.001883
Upvotes: 1
Reputation: 76927
Since, Event
is the index, you could use loc
to extract conents and place them in dict
.
In [482]: {x: df.loc[x] for x in df.index.unique()}
Out[482]:
{'Event1': DateTime ModFlow(cfs) ObsFlow(cfs) ModVol(f3) ObsVol(f3)
Event
Event1 8/15/2016 11.859260 0.000000 0.039531 0.000000
Event1 8/15/2016 30.059230 0.000000 0.100197 0.000000
Event1 8/15/2016 31.101180 0.000000 0.103671 0.000000
Event1 8/15/2016 32.174440 0.000000 0.107248 0.000000
Event1 8/15/2016 0.678317 0.565016 0.002261 0.001883,
'Event10': DateTime ModFlow(cfs) ObsFlow(cfs) ModVol(f3) ObsVol(f3) Event
Event10 6/23/2016 0.557357 0.481424 0.001858 0.001605
Event10 6/23/2016 0.553690 0.354489 0.001846 0.001182
Event10 6/23/2016 0.550211 0.368421 0.001834 0.001228
Event10 6/23/2016 0.569802 0.501548 0.001899 0.001672
Event10 6/23/2016 0.752537 0.879257 0.002508 0.002931,
'Event11': DateTime ModFlow(cfs) ObsFlow(cfs) ModVol(f3) ObsVol(f3) Event
Event11 6/10/2016 0.659315 0.614551 0.002198 0.002049
Event11 6/10/2016 0.662112 0.840557 0.002207 0.002802
Event11 6/10/2016 0.657809 0.817338 0.002193 0.002724
Event11 6/10/2016 0.658195 0.871517 0.002194 0.002905,
'Event12': DateTime ModFlow(cfs) ObsFlow(cfs) ModVol(f3) ObsVol(f3) Event
Event12 4/26/2016 2.307288 2.588235 0.007691 0.008627
Event12 4/26/2016 2.366998 3.091331 0.007890 0.010304
Event12 4/26/2016 2.494073 3.278638 0.008314 0.010929
Event12 4/26/2016 2.746868 3.083591 0.009156 0.010279
Event12 4/26/2016 3.146326 2.877709 0.010488 0.009592
Event12 4/26/2016 4.090476 2.354489 0.013635 0.007848}
Details:
In [489]: df.index.unique()
Out[489]: array(['Event1', 'Event10', 'Event11', 'Event12'], dtype=object)
In [487]: df_split = {x: df.loc[x] for x in df.index.unique()}
In [488]: df_split['Event1']
Out[488]:
DateTime ModFlow(cfs) ObsFlow(cfs) ModVol(f3) ObsVol(f3)
Event
Event1 8/15/2016 11.859260 0.000000 0.039531 0.000000
Event1 8/15/2016 30.059230 0.000000 0.100197 0.000000
Event1 8/15/2016 31.101180 0.000000 0.103671 0.000000
Event1 8/15/2016 32.174440 0.000000 0.107248 0.000000
Event1 8/15/2016 0.678317 0.565016 0.002261 0.001883
Upvotes: 5