小编Gin*_*aze的帖子

将序列四舍五入为 N 位有效数字

我有一个浮点数数据框,我需要创建一个函数,该函数将采用一列并将所有值四舍五入为 N 个有效数字

所以该列可能看起来像这样:

123.949
23.87 
1.9865
0.0129500
Run Code Online (Sandbox Code Playgroud)

如果我想四舍五入到 3 个有效数字,我会将列和 3 传递给函数来得到这个

124.0
23.9
1.99
0.013
Run Code Online (Sandbox Code Playgroud)

如何在不循环列的情况下有效地完成此操作?

我有一个方程可以计算一个数字的有效数字

round(x, N-int(floor(log10(abs(x))))
Run Code Online (Sandbox Code Playgroud)

但它不适用于系列或数据框

python math rounding pandas

8
推荐指数
1
解决办法
1万
查看次数

将颜色映射到plotly go.饼图中的标签

我正在使用 make_subplots 和 go.Pie 绘制一系列 3 个饼图。我想最终将它们放入破折号应用程序中,用户可以在其中过滤数据并且数字将更新。如何将特定颜色映射到变量,以便男性始终为蓝色,女性始终为粉红色等。您可以使用 color_discrete_map 的plotlyexpress 来完成此操作,但据我所知,plotlyexpress 不支持子图。

这是我制作性别饼图的示例 df。其他 dfs 具有相同的格式,只是值不同。

    Gender  ACC_ID  percent
0   Female  57647   57.0
1   Male    37715   37.0
2   Other   5875    6.0
Run Code Online (Sandbox Code Playgroud)

这是我用来制作数字的代码

fig = make_subplots(rows=1, cols=3, specs=[[{'type':'domain'}, {'type':'domain'},{'type':'domain'}]])
fig.add_trace(go.Pie(labels=age["Age_Group"], values=age["percent"],customdata=age["ACC_ID"], textinfo='label+percent',insidetextorientation='horizontal', textfont=dict(color='#000000'), marker_colors=px.colors.qualitative.Plotly),
              1, 1)
fig.add_trace(go.Pie(labels=gender["Gender"], values=gender["percent"], customdata=gender["ACC_ID"],textinfo='label+percent',insidetextorientation='horizontal',textfont=dict(color='#000000'),marker_colors=px.colors.qualitative.Plotly),
              1, 2)
fig.add_trace(go.Pie(labels=sample["Sample_Type"], values=sample["percent"], customdata=sample["ACC_ID"],textinfo='label+percent',texttemplate='%{label}<br>%{percent:.1%f}',insidetextorientation='horizontal',textfont=dict(color='#000000'),marker_colors=px.colors.qualitative.Prism),
              1, 3)


fig.update_traces(hole=.4, hoverinfo='label+percent', hovertemplate="<b>%{label}</b><br>Percent: %{percent}<br>Total: %{customdata}<extra></extra>")

fig.update_layout(
    showlegend=False,
    uniformtext_minsize=14, 
    uniformtext_mode='hide',

    annotations=[dict(text='Age', x=0.13, y=0.5, font_size=20, showarrow=False, font=dict(color="black")),
                 dict(text='Gender', x=0.5, y=0.5, font_size=20, showarrow=False,font=dict(color="black")),
                 dict(text='Sample', x=0.879, y=0.5, font_size=20, showarrow=False,font=dict(color="black"))])
    

plot(fig)
Run Code Online (Sandbox Code Playgroud)

python plotly plotly-dash

3
推荐指数
2
解决办法
1万
查看次数

向还没有后缀的列名称添加后缀

我有一个数据框,其中包含类似的列

Name  Date  Date_x  Date_y  A  A_x  A_y..
Run Code Online (Sandbox Code Playgroud)

我需要将 _z 添加到还没有 _x 或 _y 的列(名称列除外)。所以,我希望输出类似于

Name  Date_z  Date_x  Date_y  A_z  A_x  A_y...
Run Code Online (Sandbox Code Playgroud)

我试过了

df.iloc[:,~df.columns.str.contains('x|y|Name')]=df.iloc[:,~df.columns.str.contains('x|y|Name')].add_suffix("_z")
# doesn't add suffixes and replaces columns with all nans

df.columns=df.columns.map(lambda x : x+'_z' if "x" not in x or "y" not in x else x) 
#many variations of this but seems to add _z to all of the column names
Run Code Online (Sandbox Code Playgroud)

python python-3.x pandas

2
推荐指数
1
解决办法
627
查看次数

标签 统计

python ×3

pandas ×2

math ×1

plotly ×1

plotly-dash ×1

python-3.x ×1

rounding ×1