JeremyD
JeremyD

Reputation: 125

Subsetting column from pandas dataframe with indexing

I have a large data set in a pandas dataframe I would like to subset in order to further manipulate.

For example, I have a df that looks like this:

Sample  Group   AMP         ADP         ATP
1       A       0.3840396   0.55635504  0.5844648
2       A       0.3971521   0.57851902  -0.24603208
3       A       0.4578926   0.68118957  0.19129746
4       B       0.400222    0.58370811  0.01782915
5       B       0.4110945   0.60208593  -0.6285537
6       B       0.3307011   -0.82615087 -0.25354715
7       C       0.3485679   -0.79597002 -0.17294609
8       C       0.3408411   -0.8090222  0.76138965
9       C       0.3856457   -0.73333568 0.27364299

Lets say I want to make a new dataframe df2 from df that contains only the samples in group B and only the corresponding values for ATP. I should be able to do this from indexing alone.(?)

I would like to do something like this:

df2 = df[(df['Group']=='B') & (df['ATP'])]

I know df['ATP'] obviously is not the correct way to do this. Printing df2 yields this:

   Sample Group       AMP       ADP       ATP
   4      B           0.400222  0.583708  0.017829
   5      B           0.411094  0.602086 -0.628554
   6      B           0.330701 -0.826151 -0.253547

Ideally, df2 would look like this:

   Sample Group    ATP
   4      B        0.017829
   5      B       -0.628554
   6      B       -0.253547

Is there a way to do this without resorting to some convoluted looping or simply manually deleting the unwanted columns and their values?

Thanks!!!

Upvotes: 2

Views: 68

Answers (1)

ragesz
ragesz

Reputation: 9527

df2 = df.loc[df['Group'] == 'B', ['Sample', 'Group', 'ATP']]

Upvotes: 2

Related Questions