将dataframe的列值聚合到新的datafram

2024-05-16 13:52:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数据框,其中涉及供应商,产品,在一个市场上的各种上市价格和其他列值。你知道吗

Dataframe

我需要一个数据框,其中有唯一的供应商,产品数量,他们的产品清单,平均价格/产品和(平均*销售数量)的总和作为不同的列。你知道吗

像这样的-

Result

制作这个新数据帧的最佳方法是什么?你知道吗

谢谢!你知道吗


Tags: 数据方法数量市场产品价格供应商总和
3条回答

您可以使用pandaspivot_table来实现这一点。这是一个基于你的数据的例子。你知道吗

import pandas as pd
import numpy as np

>>> f = pd.pivot_table(d, index=['Vendor', 'Sales'], values=['Price', 'Product'], aggfunc={'Price': np.sum, 'Product':np.ma.count}).reset_index()

>>> f['Avg Price/Product'] = f['Price']/f['Product']

>>> f['H Factor'] = f['Sales']*f['Avg Price/Product']

>>> f.drop('Sales', axis=1)

  Vendor  Price  Product  Avg Price/Product  H Factor
0      A    121        4              30.25    6050.0
1      B     12        1              12.00    1440.0
2      C     47        2              23.50     587.5
3      H     45        1              45.00    9000.0

首先用Price表示多个列Number of Sales,然后使用^{}和聚合函数表示列名字典,然后用maprename表示列中的多索引。地址:

df['Number of Sales'] *=  df['Price']

d1 = {'Product':'size', 'Price':['sum', 'mean'], 'Number of Sales':'mean'}
df = df.groupby('Vendor').agg(d1)
df.columns = df.columns.map('_'.join)
d = {'Product_size':'No. of Product',
     'Price_sum':'Sum of Prices',
     'Price_mean':'Mean of Prices',
     'Number of Sales_mean':'H Factor'
     }
df = df.rename(columns=d).reset_index()
print (df)
  Vendor  No. of Product  Sum of Prices  Mean of Prices  H Factor
0      A               4            121           30.25    6050.0
1      B               1             12           12.00    1440.0
2      C               2             47           23.50     587.5
3      H               1             45           45.00    9000.0

您可以使用groupby()执行此操作,如下所示:

df.groupby('Vendor').agg({'Products': 'count', 'Price': ['sum', 'mean']})

那只是三列,但你可以算出其余的。你知道吗

相关问题 更多 >