Skip to content

Instantly share code, notes, and snippets.

@garaud
Last active May 31, 2017 20:55
Show Gist options
  • Save garaud/2bff693fe8aeb10d1813bd205d4ac9a5 to your computer and use it in GitHub Desktop.
Save garaud/2bff693fe8aeb10d1813bd205d4ac9a5 to your computer and use it in GitHub Desktop.
Rank some elements by ascending date
# coding: utf-8
"""rank by ascending date
"""
from datetime import date
from faker import Factory
import pandas as pd
import numpy as np
FAKE = Factory().create()
def fake_id(number):
f = lambda : FAKE.lexify('???').lower() + FAKE.numerify('#')
possible = [f() for _ in range(number // 10)]
return np.random.choice(possible, size=number).tolist()
def fake_date(number):
f = lambda: FAKE.date_time_this_decade().date()
return [f() for _ in range(number)]
def count_demande(df, idx, date):
return len(df[(df.idx == idx) & (df.date < date)])
if __name__ == '__main__':
nrows = 100
df = pd.DataFrame({"idx": fake_id(nrows),
"date": fake_date(nrows)})
df = df.reindex_axis(['idx', 'date'], axis=1)
df['date'] = df['date'].apply(pd.Timestamp)
df['nb_demande'] = df.apply(lambda row: count_demande(df, row.idx, row.date), axis=1)
# or
df['nb_demande_bis'] = df.groupby('idx')['date'].rank()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment