Home > other >  pandas create a subset according to a value in a column
pandas create a subset according to a value in a column

Time:12-01

I this dataframe:

86,1/28/2004 0:00:00,16.9
86,5/25/2004 0:00:00,17.01
86,7/22/2004 0:00:00,17.06
87,11/15/2004 0:00:00,7.39
87,3/14/2005 0:00:00,7.59
86,11/15/2004 0:00:00,17.29
86,3/14/2005 0:00:00,17.38
86,4/19/2005 0:00:00,17.43
86,5/19/2005 0:00:00,17.28
87,1/22/2004 0:00:00,7.44
87,5/13/2004 0:00:00,7.36

I would like to work on two separate dataframe according to the value (id) of the first column. Ideally, I would like to have:

87,11/15/2004 0:00:00,7.39
87,3/14/2005 0:00:00,7.59
87,1/22/2004 0:00:00,7.44
87,5/13/2004 0:00:00,7.36

and

86,1/28/2004 0:00:00,16.9
86,5/25/2004 0:00:00,17.01
86,7/22/2004 0:00:00,17.06
86,11/15/2004 0:00:00,17.29
86,3/14/2005 0:00:00,17.38
86,4/19/2005 0:00:00,17.43
86,5/19/2005 0:00:00,17.28

As you can see I have one dataframe with all 87 in the first column and another with 86.

This is how I read the dataframe:

dfr = pd.read_csv(fname,sep=',',index_col=False,header=None)

I think that groupby is not the right options, if I have understood correctly the command.

I was thinking about query as:

aa = dfr.query(dfr.iloc[:,0]==86)

However, I have this error:

 expr must be a string to be evaluated, <class 'pandas.core.series.Series'> given

Thnaks for any kind of help.

CodePudding user response:

You can simply slice your dataframe:

df_86 = df.loc[df['ColName'] == 86,:]

CodePudding user response:

Another way to do it dynamically without having to specify the group beforehand.

df = pd.DataFrame({'ID': np.repeat([1, 2, 3], 4), 'col2': np.repeat([10, 11, 12], 4)})

Get the unique groupings:

groups = df['ID'].unique()

Create an empty dict to store new data frames

new_dfs = {}

Loop through and create new data frames from the slice:

for group in groups:
    name = "ID"   str(group)
    new_dfs[name] = df[df['ID'] == group]

new_dfs['ID1']

Which gives:

ID  col2
0   1   10
1   1   10
2   1   10
3   1   10
  • Related