>>> import pandas as pd
>>> import csv
>>> pd.Series([my_list]).to_csv('output.tsv',sep='\t',index=False,header=False, quoting=csv.QUOTE_NONE)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: to_csv() got an unexpected keyword argument 'quoting'
Run Code Online (Sandbox Code Playgroud)
如果没有引用参数,它就可以工作.
pd.Series([my_list]).to_csv('output.tsv',sep='\t',index=False,header=False)
Run Code Online (Sandbox Code Playgroud)
但这与我的预期用法不符.
为了让事情更加令人困惑,当我以这种方式写出一张表时,没有引号,也没有错误:
my_dataframe.to_csv('output2.tsv',sep='\t', quoting=csv.QUOTE_NONE)
Run Code Online (Sandbox Code Playgroud)
知道发生了什么事吗?
在 bash 中,如果启用set -x,脚本会将所有命令的评估形式回显到 stderr 日志。在 tcsh 中,这同样是通过set echo. R 有类似的东西吗?如果我有更详细的脚本命令在运行时的外观输出,而不必手动print或cat每一个输出,这将有助于调试。
给定子文档列表,您如何根据某些条件选择插入主文档?
在我的用例中,我将一个数据集中的未知条目与第二个数据集中的所需条目进行匹配.第二个数据集具有与每个条目相关联的子文档.如果找到匹配项,我想要包含其关联的子文档.
在其最基本的形式,这个伪代码显示了什么,我想实现(被这个问题启发要点这里):
```{r, eval=TRUE}
child_docs <- setNames(c(TRUE, FALSE, TRUE, FALSE),
c("doc1.Rmd", "doc2.Rmd","doc3.Rmd","doc4.Rmd"))
for(i in seq_along(child_docs)){
file <- names(child_docs)[i]
eval_status <- child_docs[i]
```{r child = file, eval = eval_status}
```
}
```
Run Code Online (Sandbox Code Playgroud)
或者,更简单:
```{r}
child_docs <- c("child/doc1.Rmd", "child/doc2.Rmd","child/doc3.Rmd","child/doc4.Rmd")
```
```{r child = child_docs}
```
Run Code Online (Sandbox Code Playgroud)
我也试过这些,但它们没有用(RMarkdown代码块):
```{r}
child_docs <- c("child/doc1.Rmd", "child/doc2.Rmd","child/doc3.Rmd","child/doc4.Rmd")
for(i in seq_along(child_docs)){
doc <- child_docs[i]
knit_child(doc)
}
```
Run Code Online (Sandbox Code Playgroud)
(直接在RMarkdown文档中):
`for(i in seq_along(child_doc)){ doc <- child_doc[i]; knit_child(doc)}`
Run Code Online (Sandbox Code Playgroud)
作为参考,这里的手册(在LaTeX中)就在这里.
I found the documentation for pandas.DataFrame.pop, but after trying it and examining the source code, it does not seem to do what I want.
If I make a dataframe like this:
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(10,6))
# Make a few areas have NaN values
df.iloc[1:3,1] = np.nan
df.iloc[5,3] = np.nan
df.iloc[7:9,5] = np.nan
>>> df
0 1 2 3 4 5
0 0.772762 -0.442657 1.245988 1.102018 -0.740836 1.685598
1 -0.387922 NaN -1.215723 …Run Code Online (Sandbox Code Playgroud) 我有一个工作流程,我需要在R中生成一些数据,生成数据图,保存图,然后再渲染它们.我正在使用SQLite.它工作正常,但仅适用于ggplot2情节.当我尝试保存并重新渲染基础R图时,它不起作用.有任何想法吗?使用R版本3.3.0.这是我的代码:
library("ggplot2")
library("RSQLite")
# test data
dat <- data.frame(x = rnorm(50, 1, 6), y = rnorm(50, 1, 8))
# make ggplot
g <- ggplot(dat, aes(x = x, y = y)) + geom_point()
# make base plot
pdf("test.pdf") # need open graphics device to record plot headlessly
dev.control(displaylist="enable")
plot(dat)
p <- serialize(recordPlot(), NULL)
dev.off()
# make data frame for db insertion
df1 <- data.frame(baseplot = I(list(p)), ggplot = I(list(serialize(g, NULL))))
# setup db
con <- dbConnect(SQLite(), ":memory:")
dbGetQuery(con, 'create …Run Code Online (Sandbox Code Playgroud) 在R Markdown中,我可以在R代码块中设置一个对象,然后在我的文档正文中具有该对象的值,如下所示:
```{r}
TMP<-"Blue"
TMP
```
The sky is `r TMP`
Run Code Online (Sandbox Code Playgroud)
我编译的PDF文档中的输出如下所示:
TMP<-"Blue"
TMP
## [1] "Blue"
The sky is Blue
Run Code Online (Sandbox Code Playgroud)
此功能非常有用.但是,我不想仅限于使用R代码.我希望能够使用其他语言在代码块中设置对象,然后以相同的方式在文本中调用它们的值.
RMarkdown + knitr很好地允许你用其他语言编写和编译这些代码块,但是我没有找到任何方法在我的文档文本中调用这些对象的值,就像在RMarkdown中使用这种格式一样来自LaTeX的\ Sexpr {}函数.如果它更容易,我愿意使用不同的文档系统来实现这一目标.我已经看到了这样的问题这样,但是这根本就不是有用,因为我将使用的脚本是更长的时间,比小单行这样的更复杂.
这是一个完整的RMarkdown文档,详细说明了R的当前行为,以及Python等所需的(相同)行为.
title: "SAMPLE"
author: "me"
date: "September 21, 2015"
output:
pdf_document:
keep_tex: yes
---
```{r}
TMP<-"Blue"
TMP
```
You can insert the value of an object created with an R code chunk into text like this:
The sky is `r TMP`
```{r,engine='python'}
COLOR = "red"
print COLOR
```
You cannot do …Run Code Online (Sandbox Code Playgroud) R Markdown现在可以选择在.Rmd文档中自动显示或隐藏代码块.但是,这似乎只适用于R代码块.
---
output:
html_document:
code_folding: hide
---
```{r}
print("This code chunk will be hidden")
```
```{r, engine='bash'}
echo "This code chunk will not be hidden"
```{r, engine='python'}
print "Will this code chunk be hidden?"
```
```{r}
system('uname -srv',intern=T)
sessionInfo()
```
Run Code Online (Sandbox Code Playgroud)
我能够提出的唯一解决方案是隐藏空白选项卡后面的代码
---
output:
html_document:
code_folding: hide
---
```{r}
print("This code chunk will be hidden")
```
# Source code {.tabset .tabset-pills}
## Hide Code
## Show Code
```{r, engine='bash'}
echo "This code chunk will not be hidden"
``` …Run Code Online (Sandbox Code Playgroud) 在使用来自子解析器的子命令后,如何允许添加顶级程序参数?
我有一个包含多个子解析器的程序,允许使用子命令来更改程序的行为。这是如何设置的示例:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import argparse
def task_a():
print('did task_a')
def task_c():
print('did task_c')
def task_d():
print('did task_d')
def run_foo(args):
a_arg = args.a
c_arg = args.c
if a_arg:
task_a()
if c_arg:
task_c()
def run_bar(args):
a_arg = args.a
d_arg = args.d
if a_arg:
task_a()
if d_arg:
task_d()
def parse():
'''
Run the program
arg parsing goes here, if program was run as a script
'''
# create the top-level parser
parser = argparse.ArgumentParser()
# add top-level …Run Code Online (Sandbox Code Playgroud) 如何获取符号链接的完整路径而不将其解析为原始文件的路径?
例如,如果我有一个这样设置的目录:
~$ tree analysis_results/
analysis_results/
`-- Sample1
`-- bar.txt -> /Users/me/foo.txt
Run Code Online (Sandbox Code Playgroud)
如果我尝试获取 的完整路径bar.txt,我最终会得到以下路径foo.txt:
~$ readlink -f analysis_results/Sample1/bar.txt
/Users/me/foo.txt
Run Code Online (Sandbox Code Playgroud)
但我真正想要的是:
/Users/me/analysis_results/Sample1/bar.txt
Run Code Online (Sandbox Code Playgroud)
有没有简单的方法来实现这一点?看起来我没有realpath可用的,不确定该程序是否执行此操作,或者是否有其他一些类似的命令。
当我pwd是这样的时候事情变得更加复杂:
/Users/me/analysis/peaks/workflow/code
Run Code Online (Sandbox Code Playgroud)
但我想阅读符号链接的完整路径,如:
/Users/me/analysis/alignment/results/Sample1/bar.txt
Run Code Online (Sandbox Code Playgroud) 此处答案中的注释说您应该能够自定义登录 Django 的日期格式:
请注意,如果您使用 dictConfig 方法来配置日志记录(例如,如果您使用的是 Django),您可以使用格式化程序的“datefmt”字典键来设置它。请参阅:Django 日志配置,日志模块:字典架构详细信息
但是,它不起作用:
# settings.py
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'django.server': { # duplicate of default for django.server
'()': 'django.utils.log.ServerFormatter',
'format': '[{server_time}] {message}',
'style': '{',
'datefmt' : '%Y-%m-%d %H:%M:%S'
}
}
Run Code Online (Sandbox Code Playgroud)
这也不起作用:
# settings.py
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'default': {
'datefmt' : '%Y-%m-%d %H:%M:%S'
},
}
Run Code Online (Sandbox Code Playgroud)
在这两种情况下,我仍然得到默认的日志日期格式:
[13/Mar/2019 21:53:05] "GET / HTTP/1.1" 200 16808
Run Code Online (Sandbox Code Playgroud)
请注意,在源代码中,datefmt应该已经传递并使用了,但似乎并非如此:
使用 Python 3.6 和 Django 2.1 …