熊猫:使用具有变量的组归纳描述性统计数据

发布于 2025-01-28 19:50:29 字数 906 浏览 1 评论 0原文

我有这样的数据框架:

input_df = pd.DataFrame({"sex": ["M", "F", "F", "M", "M"], "Class": [1, 2, 2, 1, 1], "Age":[40, 30, 30, 50, NaN]})

我想做的是根据性别和班级列将年龄的缺失价值算。 我尝试使用一个函数,条件_impute进行操作。该功能的作用是采用数据框架和条件,然后使用它根据性别和班级分组将年龄归为年龄。但是警告是,条件可以是平均值或中位数,如果不是这两个中的任何一个,则该功能必须引起错误。 所以我这样做了:

### START FUNCTION
def conditional_impute(input_df, choice='median'):
    my_df = input_df.copy()
    # if choice is not median or mean, raise valueerror
    if choice == "mean" or choice == "median":
        my_df['Age'] = my_df['Age'].fillna(my_df.groupby(["Sex","Pclass"])['Age'].transform(choice))
    else:
        raise ValueError()    
    # round the values in Age colum
    my_df['Age'] = round(my_df['Age'], 1)
    return my_df
### END FUNCTION

但是当我打电话时我会遇到错误。

conditional_impute(train_df, choice='mean')

我可能做错了什么?我真的无法解决这个问题。

I have a data frame like this:

input_df = pd.DataFrame({"sex": ["M", "F", "F", "M", "M"], "Class": [1, 2, 2, 1, 1], "Age":[40, 30, 30, 50, NaN]})

What I want to do is to impute the missing value for the age based on the sex and class columns.
I have tried doing it with a function, conditional_impute. What the function does is take a data frame and a condition and then use it to impute the age based on the sex and class grouping. Butthe caveat is that the condition can either be a mean or median and if not either of these two, the function has to raise an error.
So I did this:

### START FUNCTION
def conditional_impute(input_df, choice='median'):
    my_df = input_df.copy()
    # if choice is not median or mean, raise valueerror
    if choice == "mean" or choice == "median":
        my_df['Age'] = my_df['Age'].fillna(my_df.groupby(["Sex","Pclass"])['Age'].transform(choice))
    else:
        raise ValueError()    
    # round the values in Age colum
    my_df['Age'] = round(my_df['Age'], 1)
    return my_df
### END FUNCTION

But I am getting an error when I call it.

conditional_impute(train_df, choice='mean')

What could I possibly be doing wrong? I really cannot get a handle on this.

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

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

发布评论

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

评论(1

久而酒知 2025-02-04 19:50:29

如果给出正确的输入,则输出恰好...

# Fixed input to match function:
df = pd.DataFrame({"Sex": ["M", "F", "F", "M", "M"], "Pclass": [1, 2, 2, 1, 1], "Age":[40, 30, 30, 50, np.nan]})

def conditional_impute(input_df, choice='median'):
    my_df = input_df.copy()
    # if choice is not median or mean, raise valueerror
    if choice == "mean" or choice == "median":
        my_df['Age'] = my_df['Age'].fillna(my_df.groupby(["Sex","Pclass"])['Age'].transform(choice))
    else:
        raise ValueError()    
    # round the values in Age colum
    my_df['Age'] = round(my_df['Age'], 1)
    return my_df

conditional_impute(df, choice='mean')

输出:

  Sex  Pclass   Age
0   M       1  40.0
1   F       2  30.0
2   F       2  30.0
3   M       1  50.0
4   M       1  45.0

If you give the right inputs, it outputs just fine...

# Fixed input to match function:
df = pd.DataFrame({"Sex": ["M", "F", "F", "M", "M"], "Pclass": [1, 2, 2, 1, 1], "Age":[40, 30, 30, 50, np.nan]})

def conditional_impute(input_df, choice='median'):
    my_df = input_df.copy()
    # if choice is not median or mean, raise valueerror
    if choice == "mean" or choice == "median":
        my_df['Age'] = my_df['Age'].fillna(my_df.groupby(["Sex","Pclass"])['Age'].transform(choice))
    else:
        raise ValueError()    
    # round the values in Age colum
    my_df['Age'] = round(my_df['Age'], 1)
    return my_df

conditional_impute(df, choice='mean')

Output:

  Sex  Pclass   Age
0   M       1  40.0
1   F       2  30.0
2   F       2  30.0
3   M       1  50.0
4   M       1  45.0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文