Pandas Dataframe 输出两位小数

发布于 2025-01-18 05:40:40 字数 773 浏览 0 评论 0原文

我有以下代码,并希望将输出(坡度,截距,R_Value,p_value,str_err)折叠成2个小数点。我该如何在此代码中进行?


for x_col in x_vars:
    for y_col in y_vars:
        
        result_dict = {}
        
        temp_df = df[[x_col, y_col]]
        temp_df = temp_df.dropna(how='any')
        
        print(x_col)
        x = temp_df[x_col]
        y = temp_df[y_col]
        
        slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)
        result_dict['x_col'] = x_col
        result_dict['y_col'] = y_col
        
        result_dict["slope"] =slope
        result_dict["intercept"] =  intercept
        result_dict["r-value"] = r_value,
        result_dict["p-value"] = p_value, 
        result_dict["std"] = std_err
        
        result_list.append(result_dict)

I have below code and want to round the output (slope, intercept, r_value, p_value, str_err) into 2 decimal places. How can I do it within this code?


for x_col in x_vars:
    for y_col in y_vars:
        
        result_dict = {}
        
        temp_df = df[[x_col, y_col]]
        temp_df = temp_df.dropna(how='any')
        
        print(x_col)
        x = temp_df[x_col]
        y = temp_df[y_col]
        
        slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)
        result_dict['x_col'] = x_col
        result_dict['y_col'] = y_col
        
        result_dict["slope"] =slope
        result_dict["intercept"] =  intercept
        result_dict["r-value"] = r_value,
        result_dict["p-value"] = p_value, 
        result_dict["std"] = std_err
        
        result_list.append(result_dict)

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

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

发布评论

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

评论(2

无法言说的痛 2025-01-25 05:40:40

您可以使用 round() 直接对这些值进行四舍五入。

for x_col in x_vars:
    for y_col in y_vars:
        
        result_dict = {}
        
        temp_df = df[[x_col, y_col]]
        temp_df = temp_df.dropna(how='any')
        
        print(x_col)
        x = temp_df[x_col]
        y = temp_df[y_col]
        
        slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)
        result_dict['x_col'] = x_col
        result_dict['y_col'] = y_col
        
        result_dict["slope"] = round(slope, 2)
        result_dict["intercept"] = round(intercept, 2)
        result_dict["r-value"] = round(r_value, 2)
        result_dict["p-value"] = round(p_value, 2) 
        result_dict["std"] = round(std_err, 2)
        
        result_list.append(result_dict)

或者,如果要将结果列表转换为数据帧,则可以在列或整个数据帧上使用 pandas.round() 来舍入值(请参阅此处:https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.round.html

df.round(2)

这个将四舍五入数据框中的值精确到小数点后两位。如果要排除列,请使用 loc[]。

df.loc[:, ['slope', 'intercept', 'r-value', 'p-value', 'std']] = \
    df[['slope', 'intercept', 'r-value', 'p-value', 'std']].round(2)

You can use round() to directly round these values in place.

for x_col in x_vars:
    for y_col in y_vars:
        
        result_dict = {}
        
        temp_df = df[[x_col, y_col]]
        temp_df = temp_df.dropna(how='any')
        
        print(x_col)
        x = temp_df[x_col]
        y = temp_df[y_col]
        
        slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)
        result_dict['x_col'] = x_col
        result_dict['y_col'] = y_col
        
        result_dict["slope"] = round(slope, 2)
        result_dict["intercept"] = round(intercept, 2)
        result_dict["r-value"] = round(r_value, 2)
        result_dict["p-value"] = round(p_value, 2) 
        result_dict["std"] = round(std_err, 2)
        
        result_list.append(result_dict)

Alternatively, if you are turning results list into a dataframe, you can use pandas.round() on a column or the entire dataframe to round the values (see here: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.round.html)

df.round(2)

This will round all values in the dataframe to 2 decimal places. If you want to exclude columns use a loc[].

df.loc[:, ['slope', 'intercept', 'r-value', 'p-value', 'std']] = \
    df[['slope', 'intercept', 'r-value', 'p-value', 'std']].round(2)
土豪我们做朋友吧 2025-01-25 05:40:40

您可以将函数round(变量,number_of_decimal_places)用于圆形变量。

在您的用例中,您可以使用以下语法来绕过字典值:

#initialize empty dictionary for the rounded values
res_dict = dict()

for key in result_dict:
    if 'col' not in key:
        res_dict[key] = round(result_dict[key], 2)
    else:
        res_dict[key] = result_dict[key]

result_list.append(res_dict)

You can use the function round(variable,number_of_decimal_places) to round variables.

In your use case, you can use the following syntax to round the dictionary values:

#initialize empty dictionary for the rounded values
res_dict = dict()

for key in result_dict:
    if 'col' not in key:
        res_dict[key] = round(result_dict[key], 2)
    else:
        res_dict[key] = result_dict[key]

result_list.append(res_dict)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文