-1
I have a dataframe:
codcliente contrato
7 1180000007004
7 1180000007003
7 1090000007038
2 1090000007035
2 1090000007029
... ...
3 2023885638001
3 1073885769001
3 1093885790001
3 1183885847001
3 6163886005001
and I want to add a contract_name column or modify the contracts column for q the contract names are 01,02,03,04,up to 05 but the number of contracts per customer may be different for each client from (1 up to 5) . Resulting the following dataframe:
codcliente contrato
7 1
7 2
7 3
2 1
2 2
... ...
3 1
3 2
3 3
3 4
3 5
What I did, but it’s too slow for a 2 million record dataframe
def re_nomear(df):
df_ultimos_contratos = []
codigos_clientes = df.codcliente.unique()
for i in codigos_clientes:
x = df[df["codcliente"] == i].copy()
x["contrato_rotulado"] = [i for i in range(x.shape[0])]
df_ultimos_contratos.append(x)
df_ultimos_contratos = pd.concat(df_ultimos_contratos)
return df_ultimos_contratos
that’s right, thank you very much
– Filomena Vera
Good that helped. Enjoy and read about how to say thank you on Stack Overflow
– Paulo Marques