从 *args 系列创建数据帧
我读取了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论