Splitting and extracting dataframes by categories in a column: python [duplicate]










0
















This question already has an answer here:



  • Looping over groups in a grouped dataframe

    2 answers



  • Iterating over groups in a dataframe [duplicate]

    1 answer



I am trying to write a function which will split a dataframe "df" into smaller dataframes df1, df2...dfn such that all records with the same ID (from column "UNIT-ID") are grouped together and stored in those smaller dataframes, in the folder respectively. I want them named as df1,df2..dfn since I plan to iterate over those n dataframes individually and use them to train my neural network model. I am also looking to write the neural network model as a function and apply the function over these split dataframes.



I have checked this and this but it talks about groupby and iterating over them. I want the extraction of the groups to be done within the same function without having to reference it or extract it with another line of code outside the function, as described in the mentioned links



Below is an example of how my data looks and how I want it to be split.



Input:

UNIT-ID Q1 Q2 Q3
110-P1 37 487 0
110-P1 31 140 1
110-P1 46 214 1
110-P2 29 287 1
110-P2 45 131 1
110-P2 39 260 0
110-P5 13 413 1
110-P5 13 259 1
110-P5 16 226 1
110-P5 49 456 1
110-P5 39 275 1
110-P10 41 189 0
110-P10 32 269 0
110-P10 31 123 1
110-P10 32 487 0
110-P10 24 398 0
110-15 23 346 0
110-15 31 419 1
110-15 37 287 0
110-15 36 228 1
110-15 48 309 1

Output:
df1:
UNIT-ID Q1 Q2 Q3
110-P1 37 487 0
110-P1 31 140 1
110-P1 46 214 1

df2:
UNIT-ID Q1 Q2 Q3
110-P2 29 287 1
110-P2 45 131 1
110-P2 39 260 0

df3:
UNIT-ID Q1 Q2 Q3
110-P5 13 413 1
110-P5 13 259 1
110-P5 16 226 1
110-P5 49 456 1
110-P5 39 275 1

df4:
UNIT-ID Q1 Q2 Q3
110-P10 41 189 0
110-P10 32 269 0
110-P10 31 123 1
110-P10 32 487 0
110-P10 24 398 0

df5:
UNIT-ID Q1 Q2 Q3
110-15 23 346 0
110-15 31 419 1
110-15 37 287 0
110-15 36 228 1
110-15 48 309 1
110-15 34 390 1
110-15 67 238 1


Any help would be appreciated!










share|improve this question















marked as duplicate by coldspeed dataframe
Users with the  dataframe badge can single-handedly close dataframe questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 12 '18 at 22:19


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















  • I have tried to specify my problem, hope it helps!

    – shweta24
    Nov 12 '18 at 22:56















0
















This question already has an answer here:



  • Looping over groups in a grouped dataframe

    2 answers



  • Iterating over groups in a dataframe [duplicate]

    1 answer



I am trying to write a function which will split a dataframe "df" into smaller dataframes df1, df2...dfn such that all records with the same ID (from column "UNIT-ID") are grouped together and stored in those smaller dataframes, in the folder respectively. I want them named as df1,df2..dfn since I plan to iterate over those n dataframes individually and use them to train my neural network model. I am also looking to write the neural network model as a function and apply the function over these split dataframes.



I have checked this and this but it talks about groupby and iterating over them. I want the extraction of the groups to be done within the same function without having to reference it or extract it with another line of code outside the function, as described in the mentioned links



Below is an example of how my data looks and how I want it to be split.



Input:

UNIT-ID Q1 Q2 Q3
110-P1 37 487 0
110-P1 31 140 1
110-P1 46 214 1
110-P2 29 287 1
110-P2 45 131 1
110-P2 39 260 0
110-P5 13 413 1
110-P5 13 259 1
110-P5 16 226 1
110-P5 49 456 1
110-P5 39 275 1
110-P10 41 189 0
110-P10 32 269 0
110-P10 31 123 1
110-P10 32 487 0
110-P10 24 398 0
110-15 23 346 0
110-15 31 419 1
110-15 37 287 0
110-15 36 228 1
110-15 48 309 1

Output:
df1:
UNIT-ID Q1 Q2 Q3
110-P1 37 487 0
110-P1 31 140 1
110-P1 46 214 1

df2:
UNIT-ID Q1 Q2 Q3
110-P2 29 287 1
110-P2 45 131 1
110-P2 39 260 0

df3:
UNIT-ID Q1 Q2 Q3
110-P5 13 413 1
110-P5 13 259 1
110-P5 16 226 1
110-P5 49 456 1
110-P5 39 275 1

df4:
UNIT-ID Q1 Q2 Q3
110-P10 41 189 0
110-P10 32 269 0
110-P10 31 123 1
110-P10 32 487 0
110-P10 24 398 0

df5:
UNIT-ID Q1 Q2 Q3
110-15 23 346 0
110-15 31 419 1
110-15 37 287 0
110-15 36 228 1
110-15 48 309 1
110-15 34 390 1
110-15 67 238 1


Any help would be appreciated!










share|improve this question















marked as duplicate by coldspeed dataframe
Users with the  dataframe badge can single-handedly close dataframe questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 12 '18 at 22:19


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















  • I have tried to specify my problem, hope it helps!

    – shweta24
    Nov 12 '18 at 22:56













0












0








0









This question already has an answer here:



  • Looping over groups in a grouped dataframe

    2 answers



  • Iterating over groups in a dataframe [duplicate]

    1 answer



I am trying to write a function which will split a dataframe "df" into smaller dataframes df1, df2...dfn such that all records with the same ID (from column "UNIT-ID") are grouped together and stored in those smaller dataframes, in the folder respectively. I want them named as df1,df2..dfn since I plan to iterate over those n dataframes individually and use them to train my neural network model. I am also looking to write the neural network model as a function and apply the function over these split dataframes.



I have checked this and this but it talks about groupby and iterating over them. I want the extraction of the groups to be done within the same function without having to reference it or extract it with another line of code outside the function, as described in the mentioned links



Below is an example of how my data looks and how I want it to be split.



Input:

UNIT-ID Q1 Q2 Q3
110-P1 37 487 0
110-P1 31 140 1
110-P1 46 214 1
110-P2 29 287 1
110-P2 45 131 1
110-P2 39 260 0
110-P5 13 413 1
110-P5 13 259 1
110-P5 16 226 1
110-P5 49 456 1
110-P5 39 275 1
110-P10 41 189 0
110-P10 32 269 0
110-P10 31 123 1
110-P10 32 487 0
110-P10 24 398 0
110-15 23 346 0
110-15 31 419 1
110-15 37 287 0
110-15 36 228 1
110-15 48 309 1

Output:
df1:
UNIT-ID Q1 Q2 Q3
110-P1 37 487 0
110-P1 31 140 1
110-P1 46 214 1

df2:
UNIT-ID Q1 Q2 Q3
110-P2 29 287 1
110-P2 45 131 1
110-P2 39 260 0

df3:
UNIT-ID Q1 Q2 Q3
110-P5 13 413 1
110-P5 13 259 1
110-P5 16 226 1
110-P5 49 456 1
110-P5 39 275 1

df4:
UNIT-ID Q1 Q2 Q3
110-P10 41 189 0
110-P10 32 269 0
110-P10 31 123 1
110-P10 32 487 0
110-P10 24 398 0

df5:
UNIT-ID Q1 Q2 Q3
110-15 23 346 0
110-15 31 419 1
110-15 37 287 0
110-15 36 228 1
110-15 48 309 1
110-15 34 390 1
110-15 67 238 1


Any help would be appreciated!










share|improve this question

















This question already has an answer here:



  • Looping over groups in a grouped dataframe

    2 answers



  • Iterating over groups in a dataframe [duplicate]

    1 answer



I am trying to write a function which will split a dataframe "df" into smaller dataframes df1, df2...dfn such that all records with the same ID (from column "UNIT-ID") are grouped together and stored in those smaller dataframes, in the folder respectively. I want them named as df1,df2..dfn since I plan to iterate over those n dataframes individually and use them to train my neural network model. I am also looking to write the neural network model as a function and apply the function over these split dataframes.



I have checked this and this but it talks about groupby and iterating over them. I want the extraction of the groups to be done within the same function without having to reference it or extract it with another line of code outside the function, as described in the mentioned links



Below is an example of how my data looks and how I want it to be split.



Input:

UNIT-ID Q1 Q2 Q3
110-P1 37 487 0
110-P1 31 140 1
110-P1 46 214 1
110-P2 29 287 1
110-P2 45 131 1
110-P2 39 260 0
110-P5 13 413 1
110-P5 13 259 1
110-P5 16 226 1
110-P5 49 456 1
110-P5 39 275 1
110-P10 41 189 0
110-P10 32 269 0
110-P10 31 123 1
110-P10 32 487 0
110-P10 24 398 0
110-15 23 346 0
110-15 31 419 1
110-15 37 287 0
110-15 36 228 1
110-15 48 309 1

Output:
df1:
UNIT-ID Q1 Q2 Q3
110-P1 37 487 0
110-P1 31 140 1
110-P1 46 214 1

df2:
UNIT-ID Q1 Q2 Q3
110-P2 29 287 1
110-P2 45 131 1
110-P2 39 260 0

df3:
UNIT-ID Q1 Q2 Q3
110-P5 13 413 1
110-P5 13 259 1
110-P5 16 226 1
110-P5 49 456 1
110-P5 39 275 1

df4:
UNIT-ID Q1 Q2 Q3
110-P10 41 189 0
110-P10 32 269 0
110-P10 31 123 1
110-P10 32 487 0
110-P10 24 398 0

df5:
UNIT-ID Q1 Q2 Q3
110-15 23 346 0
110-15 31 419 1
110-15 37 287 0
110-15 36 228 1
110-15 48 309 1
110-15 34 390 1
110-15 67 238 1


Any help would be appreciated!





This question already has an answer here:



  • Looping over groups in a grouped dataframe

    2 answers



  • Iterating over groups in a dataframe [duplicate]

    1 answer







python pandas dataframe






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 22:56







shweta24

















asked Nov 12 '18 at 22:17









shweta24shweta24

586




586




marked as duplicate by coldspeed dataframe
Users with the  dataframe badge can single-handedly close dataframe questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 12 '18 at 22:19


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by coldspeed dataframe
Users with the  dataframe badge can single-handedly close dataframe questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 12 '18 at 22:19


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • I have tried to specify my problem, hope it helps!

    – shweta24
    Nov 12 '18 at 22:56

















  • I have tried to specify my problem, hope it helps!

    – shweta24
    Nov 12 '18 at 22:56
















I have tried to specify my problem, hope it helps!

– shweta24
Nov 12 '18 at 22:56





I have tried to specify my problem, hope it helps!

– shweta24
Nov 12 '18 at 22:56












0






active

oldest

votes

















0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo