-1

So I am Currently making a python machine learning program and I want to split the data frame into two. One will be containing the values of labels equal to 1 and the other equal to zero.

This is the example of my data frame so far.

             Capital-loss  Hours-per-week  Label  
0             0              40              0  
1             0              13              1  
2             0              40              0  
3             0              40              1  
4             0              40              0  

I am currently unsure of how to split this based on the value of the label, or the best method to do this.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
badcoder11
  • 29
  • 5

2 Answers2

1

Assuming your dataframe is called df:

df_1 = df[df.Label==1]
df_0 = df[df.Label==0]
Ala Tarighati
  • 3,507
  • 5
  • 17
  • 34
1
In [32]: import pandas as pd
    ...:
    ...: mainDf = pd.DataFrame()
    ...: mainDf['Type'] = [1,23,45,56,7,6]
    ...: mainDf['Dummy'] = [1,0,1,1,0,1]

In [33]: df1 = mainDf[mainDf.Dummy == 1]
    ...: df2 = mainDf[mainDf.Dummy == 0]

In [34]: df1
Out[34]:
   Type  Dummy
0     1      1
2    45      1
3    56      1
5     6      1

In [35]: df2
Out[35]:
   Type  Dummy
1    23      0
4     7      0
Rajat Mishra
  • 3,635
  • 4
  • 27
  • 41