0

I want to get a count of the rows in dataframe:

count1 = df[(df['col1'] == xxx) & (df['col2'] == yyy)].count() 

But it still return Series. How can I get the actual count as int?

1
  • It's not working because you have more than one column, so a series is returned as noted by @EdChum. You could put ['col1'] at the end to get an int. Or anything else that effectively reduces the series to a single value. Commented Mar 18, 2015 at 17:13

3 Answers 3

1

I don't know why count() doesn't work as intended, but this does work:

df2 = df[(df['col1'] == xxx) & (df['col2'] == yyy)] count = len(df2) # int 
Sign up to request clarification or add additional context in comments.

Comments

0

Converting the resulting Series into a list could help you "tolist()"

Comments

0

Unclear what you're expecting here df.count() returns a series or df showing you the row or column count, why would you expect a simple scalar value returned?

If you wanted just the row cound then perform len(count) or count.shape[0]

example:

In [173]: df = pd.DataFrame({'a':[0,2,2,3,5], 'b':[0,1,1,3,5], 'c':np.random.randn(5)}) df Out[173]: a b c 0 0 0 -1.192011 1 2 1 0.290372 2 2 1 -0.092076 3 3 3 0.821918 4 5 5 0.551033 In [180]: count1 = df[(df['a'] == 2) & (df['b']==1)] len(count1) Out[180]: 2 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.