Notes on Python

记录一些使用Python中出现的问题, 新特性, 或是一些奇技淫巧.

Anaconda

--prefix

  • Target Environment Specification: Full path to environment location (i.e. prefix).
    1. 前缀;前置代号(置于前面的单词或字母、数字); <旧>(人名前的)称谓
    1. 在……前面加(字母或数字);加……作为前言(开场白)
1
2
conda create -p syc_py39_pyt112_cuda113 python=3.9
conda activate /data/syc/syc_py39_pyt112_cuda113

--name cannot be used with --prefix

Thomas Fauskanger, how to specify new environment location for conda create

  • conda info --envs shows blank for name but does show the full path
  • activating the environment necessitates that I type out the full path

Joshua Zastrow, how to specify new environment location for conda create

更改安装目录

服务器的root满了, 要把里面的anaconda3整个搬到data里, 详见Moving Anacoda from install dir and changing $PATH doesn't work #270.

1
2
3
4
5
6
sudo mkdir /data/anaconda3
sudo chown $USER:$USER /data/anaconda3
sudo rsync -aXS $HOME/anaconda3/. /data/anaconda3/.
mv $HOME/anaconda3 $HOME/anaconda3_back
ln -s /data/anaconda3 $HOME/anaconda3
rm -rf $HOME/anaconda3_back

Jupyter

  • Working with Jupyter notebook on a remote server: ssh -N -f -L 8888:localhost:9000 fergus@funkyserver对参数-N, -f-L的解释

Pandas

to_latex

  • pandas.io.formats.style.Styler.to_latex

    1
    2
    3
    4
    5
    6
    7
    8
    9
    styler.to_latex(
    buf='temp.tex',
    caption="Selected stock correlation and simple statistics.",
    clines="skip-last;data", # {"all;data", "all;index", "skip-last;data", "skip-last;index"}
    convert_css=True,
    position_float="centering", # {"centering", "raggedleft", "raggedright"}, optional
    multicol_align="|c|", # {"r", "c", "l", "naive-l", "naive-r"}, optional
    hrules=True,
    )

column_format

  • The LaTeX column specification placed in location: \begin{tabular}{}

  • Defaults to ‘l’ for index and non-numeric data columns, and, for numeric data columns, to ‘r’ by default, or ‘S’ if siunitx is True.

  • 注意不会判断LaTeX编译后是否报错

  • ```python styler.to_latex(buf='temp.tex', column_format='llrrrrrrrrl', convert_css=True)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15

    | Value | Explaination | Example |
    | :-----------: | :----------------------------------------------------------: | :-----------: |
    | "llrrrrrrrrl" | (default) | ![image-20221113225702852](D:\mine\Images\md-images\image-20221113225702852.png) |
    | "ll\|\|\|rr\|\|rr\|rrrr\|l" | With rules. |![image-20221114000033692](D:\mine\Images\md-images\image-20221114000033692.png)|
    | "ll\|\|rr\|rr\|rrrr\|\|l" | With rules and`multicol_align=|c|`. | ![image-20221113235858370](D:\mine\Images\md-images\image-20221113235858370.png) |



    ### `hrules`

    - Set to True to add \toprule, \midrule and \bottomrule from the {booktabs} LaTeX package. Defaults to `pandas.options.styler.latex.hrules`, which is False.

    - ```python
    styler.to_latex(buf='temp.tex', hrules=False, convert_css=True)

    Value Explanation Example
    False (default) image-20221113225702852
    True Add , and from the {booktabs} LaTeX package image-20221113225747406

clines

  • Use to control adding \cline commands for the index labels separation.

    1. [生物]渐变群,生态群
  • ```python styler.to_latex(buf='temp.tex', clines=None, convert_css=True)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15

    | Value | Explaination | Example |
    | :---------------: | :----------------------------------------------------------: | :----------------------------------------------------------: |
    | None | No cline commands are added. (default) | ![image-20221111213415302](D:\mine\Images\md-images\image-20221111213415302.png) |
    | "all;data" | A cline is added for every index value extending the width of the table, including data entries. | ![image-20221111213456222](D:\mine\Images\md-images\image-20221111213456222.png) |
    | "all;index" | As above with lines extending only the width of the index entries. | ![image-20221111213540774](D:\mine\Images\md-images\image-20221111213540774.png) |
    | "skip-last;data" | A cline is added for each index value except the last level (which is never sparsified), extending the widtn of the table. | ![image-20221111213630576](D:\mine\Images\md-images\image-20221111213630576.png) |
    | "skip-last;index" | As above with lines extending only the width of the index entries. | ![image-20221111213750279](D:\mine\Images\md-images\image-20221111213750279.png) |

    ### `multirow_align`

    - If sparsifying hierarchical MultiIndexes whether to align text centrally, at the top or bottom using the multirow package. If not given defaults to `pandas.options.styler.latex.multirow_align`, which is “c”. **If “naive” is given renders without multirow.**

    - ```python
    styler.to_latex(buf='temp.tex', multirow_align='c', convert_css=True)

    Value Explanation Example
    "c" Center aligned. (default) image-20221113225137180
    "t" Top aligned. image-20221113225205520
    "b" Bottom aligned. image-20221113225225857
    "naive" Top aligned without multirow. image-20221113225259474

multicol_align

  • If sparsifying hierarchical MultiIndex columns whether to align text at the left, centrally, or at the right. If not given defaults to pandas.options.styler.latex.multicol_align, which is “r”. If a naive option is given renders without multicol. Pipe decorators can also be added to non-naive values to draw vertical rules, e.g. “|r” will draw a rule on the left side of right aligned merged cells.

  •   styler.to_latex(buf='temp.tex', multicol_align='r', convert_css=True)
    Value Explanation Example
    "r" Right aligned. (default) image-20221113224604912
    "c" Center aligned. image-20221113224628864
    "l" Left aligned. image-20221113224646848
    "naive-r" Right aligned without multicol. image-20221113224742232
    "naive-l" Left aligned without multicol. image-20221113224724568
    "|r" Draw a rule on the left side of right aligned merged cells. image-20221113224508489
    "|c|" Draw rules on the both side of center aligned merged cells. image-20221113224809154

Troubleshooting

Ubuntu中安装包

Window里正常, 在Ubuntu配环境时发现, 在conda环境下使用pip安装, 需要选择pip, 完整的安装方法如下, 可参见Using Pip to install packages to Anaconda Environment:

  1. 使用which -a pip查看所有的pip路径, 使用which pip查看当前的pip路径, 确定别装到别的环境里去了
  2. 安装包, 大致有三种方法
    • 使用/anaconda/envs/<EnvName>/bin/pip install <PackageName>
    • 进入环境后python -m pip install <PackageName>, -m的含义为module-name, 见Meaning of python -m flag
    • 在Pycharm的Terminal里直接pip install <PackageName>
  3. 若发现环境还是装错位置了, 可能是使用了--user参数全局地安装了一些包, 要把他们都删了, 具体见local pip in conda environment checks globally and says Requirement already satisfied

Seaborn的安装

pip安装后报错load failed while importing _arpack: The specified procedure could not be found., 卸载后用conda安装就好了, 见Seaborn ImportError: DLL load failed: The specified module could not be found.