rename rows of a dataframe

Asked

Viewed 32 times

-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

1 answer

2


Use groupby() with cumcount()

Creating Dataframe

df = pd.DataFrame({"codcliente": [7,7,7,2,2,3,3,3,3,3],"contrato": [1180000007004, 1180000007003, 1090000007038, 1090000007035, 1090000007029, 2023885638001, 1073885769001, 1093885790001, 1183885847001, 6163886005001]})

Verifying Dataframe

print(df)

   codcliente       contrato
0           7  1180000007004
1           7  1180000007003
2           7  1090000007038
3           2  1090000007035
4           2  1090000007029
5           3  2023885638001
6           3  1073885769001
7           3  1093885790001
8           3  1183885847001
9           3  6163886005001

Creating new column

df['nova_coluna'] = df.groupby('codcliente').cumcount()+1

Upshot

   codcliente       contrato  nova_coluna
0           7  1180000007004            1
1           7  1180000007003            2
2           7  1090000007038            3
3           2  1090000007035            1
4           2  1090000007029            2
5           3  2023885638001            1
6           3  1073885769001            2
7           3  1093885790001            3
8           3  1183885847001            4
9           3  6163886005001            5

Browser other questions tagged

You are not signed in. Login or sign up in order to post.