Problem Concating and making a Sum in a Describe of some Columns

52 Views Asked by At

Here it´s my problem y have two dataframes. I´m wanting to only make the sum in the column ¨Cantidad¨ because as you see, the other information is the same, I am only wanting to sum the column of ¨Cantidad¨ because that column will be variable. (Here samples) :

First DF

fac

    Tarifa  Precio  Cantidad    Importe $   Porcentaje
3   Vecina      155     87       13485         49.2%
2   Misma Zona  130     72        9360         40.7%
0   Alejada     229     17        3893          9.6%
1   Grande      250      1         250          0.6%

Second DF

fac2 
Tarifa       Precio        Cantidad     Importe $   Porcentaje
2   Vecina      155             61         9455         55.5%
1   Misma Zona  130             40         5200         36.4%  
0   Alejada     229             9          2061          8.2%

I tried this with no luck:

df_concat = pd.concat([fac,fac2],axis=0)
df_grouped = df_concat.groupby(["Tarifa", "Precio"]).agg({"Cantidad": "sum"}).reset_index()

# Ordenamos el dataframe por las mismas columnas que utilizamos en el groupby
df_result = df_grouped.sort_values(["Tarifa", "Precio"])

# Mostramos el resultado
print(df_result)

The result:

    Tarifa   Precio     Cantidad
2   Vecina      155         87  
1   Misma Zona  130         72
0   Alejada     229         17  

As you see there is not sum in the column ¨Cantidad¨

Hope can you help me!

Best regards!

2

There are 2 best solutions below

0
Laurent B. On BEST ANSWER
r = (pd.concat([df1, df2], ignore_index=True)
       .groupby('Tarifa')
       .agg({'Precio': 'first', 'Cantidad': sum})
)
                                                                   
print(r)
            Precio  Cantidad
Tarifa                      
Alejada        229        26
Grande         250         1
Misma Zona     130       112
Vecina         155       148
1
Jason Yu On

Since only one column is variable, you can try doing

df = pd.concat([fac, fac2], axis=0)[['Tarifa', 'Precio', 'Cantidad']]
df_result = df.groupby(['Tarifa', 'Precio']).sum().reset_index()