从 *args 系列创建数据帧

发布于 2025-01-10 16:34:36 字数 835 浏览 0 评论 0原文

我读取了 csv 文件并创建了一个数据框。

Date  Region  Measure1  Measure2
02/21 North   0.8765    12562
04/21 North   0.1723    21861
06/21 South   0.9617    17308

然后,我想编写一个函数,该函数从该数据帧中获取几列作为 *args,并按维度列对这些数据进行分组以计算平均值。我尝试了这个:

import pandas as pd
measure_col = df['Measure1']
col1 = df['Date']
col2 = df['Region']

# Average Function
def avg(measure, *cols):
    cols = list(cols)
    data = pd.DataFrame([measure, cols], axis=1)
    return data.groupby(cols)[measure].transform("mean")

print(avg(measure_col, col1, col2))

我的目标是使用提供给 avg 函数的参数创建一个数据框。上面的代码会导致 TypeError,因为它无法连接类型列表,而只能连接 Series 或 DataFrame。如果我不转换为列表,我将无法通过 groupby 传递它。如何在函数内获取所需的数据框?

Date  Region  Measure1  
02/21 North   0.8765    
04/21 North   0.1723    
06/21 South   0.9617

谢谢

I read a csv file and create a dataframe.

Date  Region  Measure1  Measure2
02/21 North   0.8765    12562
04/21 North   0.1723    21861
06/21 South   0.9617    17308

I then want to write a function that takes in a few columns from this data frame as *args and groups that data by a dimension column to calculate the average. I tried this:

import pandas as pd
measure_col = df['Measure1']
col1 = df['Date']
col2 = df['Region']

# Average Function
def avg(measure, *cols):
    cols = list(cols)
    data = pd.DataFrame([measure, cols], axis=1)
    return data.groupby(cols)[measure].transform("mean")

print(avg(measure_col, col1, col2))

My aim here is to make a dataframe with just the arguments given to the avg function. The above code results in a TypeError as it cannot concatenate the type list but only Series or DataFrame. If I don't convert to list I am not able to pass it groupby. How can I get the required dataframe inside the function?

Date  Region  Measure1  
02/21 North   0.8765    
04/21 North   0.1723    
06/21 South   0.9617

thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文