小编Adr*_*ico的帖子

以幻灯片模式运行Jupyter单元格

您可以通过运行以下命令在活动的html设置中显示Jupyter笔记本:

$ jupyter nbconvert untitled.ipynb --to slides --post serve
Run Code Online (Sandbox Code Playgroud)

有没有办法以相同的幻灯片格式运行笔记本,以便允许您的单元格的实时演示/执行?

slideshow jupyter jupyter-notebook

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

Python / with Pandas 中的描述性统计,括号中为 std

这个问题涉及在 Python 中使用与学术出版物中找到的表格相对应的格式化输出进行描述性统计的最佳实践:平均值在下面的括号中带有各自的标准偏差。最终目标是能够以 Latex 表格格式(或其他格式、html 等)导出它。

示例(Deucherta & Eugster (2018)):

在此处输入图片说明

熊猫:

在 Pandas 中进行描述性统计的经典解决方案是使用 a 的describe()方法DataFrame

import numpy as np
import pandas as pd 

# Generate a DataFrame to have an example 
df = pd.DataFrame(
    {"Age" : np.random.normal(20,15,5), 
     "Income": np.random.pareto(1,5)*20_000 }
    )
# The describe method to get means and stds
df.describe().loc[["mean", "std"]].T
>>>
                mean            std
Age        15.322797      13.449727
Income  97755.733510  143683.686484
Run Code Online (Sandbox Code Playgroud)

我想要的是以下输出:

Age        15.32 
          (13.44)
Income  97755.73  
        (143683.68)
Run Code Online (Sandbox Code Playgroud)

有一个适用于多索引数据帧的解决方案会很好:

df2 = pd.DataFrame(
    {"Age" …
Run Code Online (Sandbox Code Playgroud)

python statistics standard-deviation pandas statsmodels

6
推荐指数
1
解决办法
1889
查看次数

如何使用对应字典重命名 pd.value_counts() 索引

我正在做一个value_counts()代表分类值的整数列。

我有一个 dict 将数字映射到与类别名称对应的字符串。

我想找到具有相应名称的索引的最佳方法。因为我对我的 4 行解决方案不满意。

我目前的解决方案

df = pd.DataFrame({"weather": [1,2,1,3]})
df
>>>
   weather
0        1
1        2
2        1
3        3

weather_correspondance_dict = {1:"sunny", 2:"rainy", 3:"cloudy"}
Run Code Online (Sandbox Code Playgroud)

现在我如何解决问题:

df_vc = df.weather.value_counts()
index = df_vc.index.map(lambda x: weather_correspondance_dict[x] )
df_vc.index = index
df_vc
>>>
sunny     2
cloudy    1
rainy     1
dtype: int64
Run Code Online (Sandbox Code Playgroud)

我对那个非常乏味的解决方案不满意,你有这种情况的最佳实践吗?

python dictionary counting dataframe pandas

5
推荐指数
1
解决办法
2040
查看次数

仅悬停在具有散景的选定行上

我正在尝试选择我的悬停显示为vline模式的行。

这是示例(取自this SO question):

days = ['2018/1/1', '2018/1/2', '2018/1/3', '2018/1/4', '2018/1/5']
data_a = [10, 34, 23, 14, 58]
data_b = [20, 13, 45, 98, 65]
data_c = [20, 23, 43, 76, 57]
df_plot = pd.DataFrame({'A': data_a, 'B': data_b, 'C': data_c}, index=days)
df_plot['dates'] = pd.to_datetime(df_plot.index, format='%Y/%m/%d')
source = ColumnDataSource(df_plot)
p = figure(x_axis_type="datetime")
p.line('dates', 'A', source=source, color='red')
p.line('dates', 'B', source=source, color='blue')
p.line('dates', 'C', source=source, color='green')
p.add_tools(HoverTool(tooltips=[("A", "@A"), ("B", "@B"), ("C", "@C")],mode = "vline"))
show(p)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

图像悬停出现在所有线条上,如果我只想悬停在红线上怎么办?

python visualization bokeh

4
推荐指数
1
解决办法
772
查看次数