如果我想在数据框中使用一些变量求和dplyr,我可以这样做:
> head(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa
> select(iris, starts_with('Petal')) %>% rowSums()
[1] 1.6 1.6 1.5 1.7 1.6 2.1 1.7 1.7 1.6 1.6 1.7 1.8 1.5 1.2 1.4 1.9 1.7 1.7 2.0 1.8 1.9 1.9 1.2 2.2 2.1 1.8 2.0 1.7 …Run Code Online (Sandbox Code Playgroud) 如果我有 myapp/models.py
from django.db import models
class FooBar(models.Model):
x = models.BooleanField()
class Foobar(models.Model):
y = models.BooleanField()
Run Code Online (Sandbox Code Playgroud)
并加入myapp到INSTALLED_APPS做一个syncdb,我只得到FooBar模型转换成数据库表。该Foobar模型被忽略。
另一个需要注意的奇怪事情是,当我们做
from myapp import models
Run Code Online (Sandbox Code Playgroud)
两者FooBar并Foobar存在它们的属性models。然而,
>>> models.FooBar.__name__
'FooBar'
>>> models.Foobar.__name__
'FooBar'
Run Code Online (Sandbox Code Playgroud)
并且两者都只是FooBar(默认情况下myapp_foobar)的 db 表的接口。
我问这个问题是因为在我看来,django 模型名称不区分大小写,但我还没有找到任何说明这一点的文档,而且这个问题的回答是 django 模型名称区分大小写。
当我打开以.tex.
如果我有一个名为的文本文件foo.txt并在 vim 中打开它并执行:set spell,我会突出显示拼写错误的单词,我可以使用]s等移动到它们,并且我可以使用z=. 如果我重命名foo.txt为foo.tex并在 vim 中打开它,:set spell什么也不]s做,什么也不做,但是z=当光标在一个单词上时输入确实提供了更正选项。
当我foo.tex在 vim 中打开并执行 时:set filetype,我得到filetype=tex. 如果我改为执行:set filetype=latex、:set spell和]s等,则一切正常。
如何进行拼写检查以处理.tex文件类型文件?请注意,我只想在明确执行时进行拼写检查:set spell,我不希望默认情况下启用它。
当我尝试在 arch Linux 上启动 realvnc-vnc-viewer 时,缺少依赖项 (libcrypt.so.1)。每次我尝试使用命令行启动 vnc-viewer 时,都会提示这些错误:vncviewer: error while loading shared libraries: libcrypt.so.1: cannot open shared object file: No such file or directory
我有 2.0 版的 libcrypt.so,但 vnc-viewer 似乎仅适用于版本 1。我已经使用 pacman 下载了 realvnc-vnc-viewer 并我无法再启动该应用程序。
有任何想法吗?
我正在使用python的sh来编写git命令.例如,我做的事情就像
import sh
git = sh.git.bake(_cwd='/some/dir/')
project_hash = git('rev-parse', 'HEAD').stdout.strip()
project_branch = git('rev-parse', '--abbrev-ref', 'HEAD').stdout.strip()
project_date = git('log', '-1', '--pretty=format:%ci').stdout.strip()
Run Code Online (Sandbox Code Playgroud)
然后我将project_hash,project_branch和project_date写入数据库等.
问题是git有时会在其输出中添加shell转义序列.例如,
print(repr(project_hash))
print(repr(project_branch))
print(repr(project_date))
Run Code Online (Sandbox Code Playgroud)
导致
'e55595222076bd90b29e184b6ff6ad66ec8c3a03'
'master'
'\x1b[?1h\x1b=\r2012-03-26 01:07:40 -0500\x1b[m\r\n\r\x1b[K\x1b[?1l\x1b>'
Run Code Online (Sandbox Code Playgroud)
前两个字符串不是问题,但最后一个字符串,即日期,具有转义序列.
有什么方法可以摆脱这些,例如要求git不输出任何转义序列?
我已经使用git log命令尝试了"--no-color"选项.这没有用.
我也很乐意在python中删除它们,但我不知道如何.我试过s.encode('ascii'),其中s是日期字符串.这并没有什么不同.
在没有shell转义序列的Python中打印stdout解决了同样的问题.建议使用python的子进程而不是sh.我可以做到
project_date = subprocess.check_output(["git", "log", "-1", "--pretty=format:%ci"], cwd='/some/dir/')
Run Code Online (Sandbox Code Playgroud)
和
print(repr(project_date))
Run Code Online (Sandbox Code Playgroud)
给
'2012-03-26 01:07:40 -0500'
Run Code Online (Sandbox Code Playgroud)
当然,这就是我想要的.但是,如果有可能我宁愿坚持使用sh,那么我想知道我是否可以使用sh来避免转义序列.
有什么建议?
假设我的 Pandas 数据框如下:
import pandas as pd
df = pd.DataFrame(
dict(ID = [1, 2, 3],
xz = [0, 1, 1],
yz = [4, 5, 6],
yx = [7, 11, 18],
xy = [10, 10, 11])
)
Run Code Online (Sandbox Code Playgroud)
如果我想选择名称包含 的所有列x,我可以执行以下操作:
df.filter(regex = 'x', axis=1)
Run Code Online (Sandbox Code Playgroud)
如果我还想按列名称进行选择,我想执行以下操作,但这是行不通的。
df.filter(items = ['ID'], regex = 'x', axis=1)
Run Code Online (Sandbox Code Playgroud)
这是行不通的,因为
TypeError: Keyword arguments `items`, `like`, or `regex` are mutually exclusive`
Run Code Online (Sandbox Code Playgroud)
这是一个可行的替代方案。
df.iloc[:,df.columns.str.contains('x') |
df.columns.str.match('ID')]
Run Code Online (Sandbox Code Playgroud)
有更好或者更推荐的方法吗?
我知道使用 R/tidyverse(即Df %>% select(ID, contains('x')). 我还知道siuba等使用 Pandasdplython …