问题:连接和在某些列的描述中进行求和

huangapple go评论59阅读模式
英文:

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

问题

这里是我的问题,我有两个数据框。

我只想在“Cantidad”列中进行求和,因为如你所见,其他信息是相同的,我只想对“Cantidad”列进行求和,因为该列会是可变的。

第一个数据框:

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%

第二个数据框:

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%

我尝试了以下代码但没有成功:

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)

结果:

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

如你所见,“Cantidad”列中没有求和。

希望可以帮到你!

最好的问候!

英文:

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!

答案1

得分: 2

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
英文:
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

答案2

得分: 1

由于只有一列是可变的,您可以尝试执行以下操作:

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

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()

huangapple
  • 本文由 发表于 2023年2月19日 07:57:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497157.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定