Atihska
Atihska

Reputation: 5126

How can I get a cell value from grouped dataframe without the index

I am using a pandas df.groupby() function to group my dataframe on a column and iterate over it as follows:

    df = pd.DataFrame({'A' : ['foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'foo'],
                       'B' : ['one', 'one', 'two', 'three', 'two', 'two', 'one', 'three'],
                       'C' : np.random.randn(8),
                       'D' : np.random.randn(8)})

""" above df looks like this
     A      B         C         D
0  foo    one -0.575010 -0.271119
1  bar    one -0.130209 -0.106217
2  foo    two  0.093987 -1.351369
3  bar  three -0.403304  0.983619
4  foo    two  0.668989  0.249099
5  bar    two  1.153876  1.407159
6  foo    one  1.453793 -0.347721
7  foo  three  0.493562 -0.051688
"""

    grouped = df.groupby('A')

    for name, group in grouped:
        print(group)
        print(group['B'])

Here print(group) returns as follows:

     A      B         C         D
1  bar    one -0.130209 -0.106217
3  bar  three -0.403304  0.983619
5  bar    two  1.153876  1.407159

And group['B'] returns

1      one
3    three
5      two

I want to get one and not the index 1 or 3 or 5 but just one, three and two which are column B values.

Here iloc won't work as indexes aren't continuous and I won't know which index is coming while iterating over the grouped dataframe.

Upvotes: 2

Views: 315

Answers (2)

cs95
cs95

Reputation: 402333

Use df.to_string with index=False:

for _, g in df.groupby('A'):
     print(g['B'].to_string(index=False))

This prints out a series but without the accompanying index. Alternatively, use g['B'].tolist(), if you want a list of values.

Upvotes: 2

jpp
jpp

Reputation: 164623

Try group['B'].values.

This will return the elements of the series as a numpy array.

Upvotes: 1

Related Questions