Skip to content Skip to sidebar Skip to footer

How To Efficiently Count The Number Of Smaller Elements For Every Element In Another Column?

I have the following df name created_utc 0 t1_cqug90j 1430438400 1 t1_cqug90k 1430438400 2 t1_cqug90z 1430438400 3 t1_cqug91c 1430438401 4 t1_cqug91e 1430

Solution 1:

I think you need sort_index for descending sorting for your previous answer:

count_utc = df.groupby('created_utc').size().sort_index(ascending=False)
print (count_utc)
created_utc
1430438401    2
1430438400    3
dtype: int64

cumulative_counts = count_utc.shift(fill_value=0).cumsum()

output = dict(zip(df['name'], df['created_utc'].map(cumulative_counts)) )

print (output)
{'t1_cqug90j': 2, 't1_cqug90k': 2, 't1_cqug90z': 2, 't1_cqug91c': 0, 't1_cqug91e': 0}

Post a Comment for "How To Efficiently Count The Number Of Smaller Elements For Every Element In Another Column?"