CadisEtRama
CadisEtRama

Reputation: 1111

Python/Pandas: Counting the number of times a value less than x appears in a column

Hi I have a file of variables that I have read in as sim.

>>sim.head()
        SIM0    212321
        SIM1    9897362
        SIM2    345
        SIM3    2345
        SIM4    79727367

I have assigned the first value of the column to original:

original=sim[0]
212321

I would like to use pandas to count the number of times a number less than 212321 appears in sim. Is there a way to do this without a loop?

Upvotes: 3

Views: 3997

Answers (1)

unutbu
unutbu

Reputation: 879471

If sim is a Series, you could do this:

import pandas as pd

sim = pd.Series([212321, 9897362, 345, 2345, 79727367],
                index=map('SIM{}'.format, range(5)))

orig = sim[0]
num_smaller_items = (sim < orig).sum()
print(num_smaller_items)
# 2

Upvotes: 3

Related Questions