在Pandas Groupby和Agg Funcion中使用if/else语句

发布于 2025-01-21 03:51:51 字数 1204 浏览 1 评论 0原文

让我们考虑下面的这个数据帧

df = pd.DataFrame([('Bike', 'Kawasaki', 130 , 186),
               ('Bike', 'Ducati Panigale' , 135 , 202),
               ('Car', 'Bugatti Chiron' , 461 , 304), 
               ('Car', 'Jaguar XJ220', 472 , 210),
               ('Bike', 'Lightning LS-218' , 137 , 218), 
               ('Car', 'Hennessey Venom GT' , 523 , 270),
               ('Bike', 'BMW S1000RR' , 174 , 188)],
              columns =('Type', 'Name', 'Length' ,'top_speed(mph)'))

df

我想做的是计算长度和最高速度的最小值、最大值、平均值、中值和标准差,但每个数值变量都有一个 If/Else 条件。

这就是我现在所做的:

df.groupby(["Type"]).agg(
{
    "Length": ["max", "min", "mean", "median", "std"],
    "top_speed(mph)": ["max", "min", "mean", "median", "std"],
},
axis=1,
)

groupby - agg

我想计算长度最小值、最大值等 if Type == Bike else None 并计算 top_speed 最小值、最大值等 if类型 == 其他汽车无。我知道我们可以使用 lambda 函数,但如果我们需要大量计算一件事,这里我必须为每个值计算 5 个值

Let's consider this dataFrame below

df = pd.DataFrame([('Bike', 'Kawasaki', 130 , 186),
               ('Bike', 'Ducati Panigale' , 135 , 202),
               ('Car', 'Bugatti Chiron' , 461 , 304), 
               ('Car', 'Jaguar XJ220', 472 , 210),
               ('Bike', 'Lightning LS-218' , 137 , 218), 
               ('Car', 'Hennessey Venom GT' , 523 , 270),
               ('Bike', 'BMW S1000RR' , 174 , 188)],
              columns =('Type', 'Name', 'Length' ,'top_speed(mph)'))

df

What i'ld like to do is compute min, max, mean, median and std for Length and top speed but with an If/Else condition for each numerical variable.

This is what i do now:

df.groupby(["Type"]).agg(
{
    "Length": ["max", "min", "mean", "median", "std"],
    "top_speed(mph)": ["max", "min", "mean", "median", "std"],
},
axis=1,
)

groupby - agg

I'ld like to compute Length min, max etc if Type == Bike else None and compute top_speed min, max etc if Type == Car else None. I know we can use a lambda function but in the case we need ton compute one thing, here i have to compute 5 values for each

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一曲爱恨情仇 2025-01-28 03:51:51

尝试:

dfs = []
for t in df["Type"].unique():
    x = df[df["Type"] == t].describe()
    # remove/rename rows here as appropriate
    # ...
    dfs.append(x.assign(Type=t).set_index("Type", append=True).unstack(level=0))

x = pd.concat(dfs)
print(x)

打印:

      Length                                                           top_speed(mph)                                                         
         25%    50%     75% count    max        mean    min        std            25%    50%    75% count    max        mean    min        std
Type                                                                                                                                          
Bike  133.75  136.0  146.25   4.0  174.0  144.000000  130.0  20.215506          187.5  195.0  206.0   4.0  218.0  198.500000  186.0  14.821156
Car   466.50  472.0  497.50   3.0  523.0  485.333333  461.0  33.080709          240.0  270.0  287.0   3.0  304.0  261.333333  210.0  47.595518

Try:

dfs = []
for t in df["Type"].unique():
    x = df[df["Type"] == t].describe()
    # remove/rename rows here as appropriate
    # ...
    dfs.append(x.assign(Type=t).set_index("Type", append=True).unstack(level=0))

x = pd.concat(dfs)
print(x)

Prints:

      Length                                                           top_speed(mph)                                                         
         25%    50%     75% count    max        mean    min        std            25%    50%    75% count    max        mean    min        std
Type                                                                                                                                          
Bike  133.75  136.0  146.25   4.0  174.0  144.000000  130.0  20.215506          187.5  195.0  206.0   4.0  218.0  198.500000  186.0  14.821156
Car   466.50  472.0  497.50   3.0  523.0  485.333333  461.0  33.080709          240.0  270.0  287.0   3.0  304.0  261.333333  210.0  47.595518
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文