小编elz*_*rdo的帖子

Pandas DataFrame按分类列排序,但按特定的类排序排序

我想通过使用选择特定列条目的Pandas数据框中的顶级条目df_selected = df_targets.head(N).

每个条目都有一个target值(按重要性顺序):

Likely Supporter, GOTV, Persuasion, Persuasion+GOTV  
Run Code Online (Sandbox Code Playgroud)

不幸的是如果我这样做

df_targets = df_targets.sort("target")
Run Code Online (Sandbox Code Playgroud)

订货会是字母(GOTV,Likely Supporter,...).

我希望有一个关键字,如list_ordering:

my_list = ["Likely Supporter", "GOTV", "Persuasion", "Persuasion+GOTV"] 
df_targets = df_targets.sort("target", list_ordering=my_list)
Run Code Online (Sandbox Code Playgroud)

为了解决这个问题,我创建了一个字典:

dict_targets = OrderedDict()
dict_targets["Likely Supporter"] = "0 Likely Supporter"
dict_targets["GOTV"] = "1 GOTV"
dict_targets["Persuasion"] = "2 Persuasion"
dict_targets["Persuasion+GOTV"] = "3 Persuasion+GOTV"
Run Code Online (Sandbox Code Playgroud)

,但它似乎是一种非pythonic方法.

建议将不胜感激!

sorting dataframe python-2.7 pandas categorical-data

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

如何在 Jupyter 中显示单元格编号?

我想知道如何在 Jupyter 笔记本中显示单元格编号。

动机:
我正在使用nbformatJupyter notebook 显示信息。

要检索文本和图像信息,我目前依赖于以下execution_count值:

def select_cell(notebook, out_value):
    """
    returns the information of a cell
    """

    # Notebook formatting defintions
    if notebook["nbformat"] < 4:
        cell_all = list(notebook["worksheets"][0]["cells"])
        key_counter = 'prompt_number'
     else:
        cell_all = list(notebook["cells"])
        key_counter = 'execution_count'

     # Finding and returning the relevant cell
     for icell, cell in enumerate(cell_all):
        try: # Not all Cells have the `key_counts`
            if cell[key_counter] == out_value:
                return cell
        except:
            None

    return None
Run Code Online (Sandbox Code Playgroud)

通过out_value我指的是在方括号中的数值在左边距这里是一个笔记本说:“出[out_value]:”。
并且notebook = …

python-2.7 jupyter-notebook

5
推荐指数
0
解决办法
1030
查看次数