Vaex 中的日期分布直方图

问题描述 投票:0回答:1

我正在尝试将here的答案转换为Vaex,这样我就可以从数据框中绘制日期的条形图/直方图。我在 groupby 之后尝试了不同的操作,例如

.sum()
等,但无法使其正常工作。 Vaex 有更好的方法来实现这一点吗?

python pandas vaex
1个回答
0
投票

您可以使用

agg.sum()
(如果您正在数数,则为
agg.count()
)。这是一个虚构销售数据的示例。请注意,我仅使用 pandas 创建要使用
vaex
:

读取的 csv 文件
import pandas as pd
import numpy as np
import vaex

np.random.seed(0)
dates = pd.date_range('20230101', periods=60) 
data = {
    'date': np.random.choice(dates, 500),
    'product_id': np.random.choice(['A', 'B', 'C'], 500),
    'quantity': np.random.randint(1, 10, 500),
    'price_per_unit': np.random.uniform(10, 50, 500)
}
pdf = pd.DataFrame(data)

csv_file_path = 'sample_sales_data.csv'
pdf.to_csv(csv_file_path, index=False)

df = vaex.from_csv(csv_file_path, parse_dates=['date'])
df['total_sales'] = df['quantity'] * df['price_per_unit']
df['year_month'] = df.date.dt.strftime('%Y-%m')
result_product = df.groupby('product_id', agg={'total_sales_sum': vaex.agg.sum(df['total_sales'])})
result_month = df.groupby('year_month', agg={'total_sales_sum': vaex.agg.sum(df['total_sales'])})

result_product_df = result_product.to_pandas_df()
result_month_df = result_month.to_pandas_df()

result_product_df, result_month_df

这给出了

(  product_id  total_sales_sum
 0          B     23406.541203
 1          A     23120.765300
 2          C     24332.454628,
   year_month  total_sales_sum
 0    2023-02     33218.240290
 1    2023-01     36190.503868
 2    2023-03      1451.016974)
© www.soinside.com 2019 - 2024. All rights reserved.