假设我有以下pandas数据帧:
df = DataFrame({'A' : [5,6,3,4], 'B' : [1,2,3, 5]})
df
A B
0 5 1
1 6 2
2 3 3
3 4 5
Run Code Online (Sandbox Code Playgroud)
我可以根据特定值进行子集化:
x = df[df['A'] == 3]
x
A B
2 3 3
Run Code Online (Sandbox Code Playgroud)
但是我如何根据值列表进行子集化? - 这样的事情:
list_of_values = [3,6]
y = df[df['A'] in list_of_values]
Run Code Online (Sandbox Code Playgroud) 我想用IPython笔记本作为交互式分析我用Biopython GenomeDiagram
模块制作的一些基因组图表的方法.虽然有关于如何matplotlib
在IPython笔记本中使用内联图形的大量文档,但GenomeDiagram使用了ReportLab工具包,我认为它不支持IPython中的内联图形.
然而,我想,解决这个问题的方法是将绘图/基因组图表写入文件,然后打开内联图像,这将具有相同的结果,如下所示:
gd_diagram.write("test.png", "PNG")
display(file="test.png")
Run Code Online (Sandbox Code Playgroud)
但是,我无法弄清楚如何做到这一点 - 或者知道它是否可行.那么有人知道图像是否可以在IPython中打开/显示?
我有一个数字列表,我想得到一个数字出现在符合特定条件的列表中的次数.我可以使用列表理解(或函数中的列表理解),但我想知道是否有人有更短的方式.
# list of numbers
j=[4,5,6,7,1,3,7,5]
#list comprehension of values of j > 5
x = [i for i in j if i>5]
#value of x
len(x)
#or function version
def length_of_list(list_of_numbers, number):
x = [i for i in list_of_numbers if j > number]
return len(x)
length_of_list(j, 5)
Run Code Online (Sandbox Code Playgroud)
有没有更浓缩的版本?
我有一个文本文件,其中列由可变数量的空格分隔.是否可以直接将此文件作为pandas数据帧加载而无需预处理文件?在pandas文档中,分隔符部分说我可以使用's*'
构造但我无法使其工作.
## sample data
head sample.txt
# --- full sequence --- -------------- this domain ------------- hmm coord ali coord env coord
# target name accession tlen query name accession qlen E-value score bias # of c-Evalue i-Evalue score bias from to from to from to acc description of target
#------------------- ---------- ----- -------------------- ---------- ----- --------- ------ ----- --- --- --------- --------- ------ ----- ----- ----- ----- ----- ----- ----- ---- ---------------------
ABC_membrane PF00664.18 275 AAF67494.2_AF170880 …
Run Code Online (Sandbox Code Playgroud) 我想使用ggplot循环遍历多个列以创建多个绘图,但在for循环中使用占位符会更改ggplot的行为.
如果我有这个:
t <- data.frame(w = c(1, 2, 3, 4), x = c(23,45,23, 34),
y = c(23,34,54, 23), z = c(23,12,54, 32))
Run Code Online (Sandbox Code Playgroud)
这很好用:
ggplot(data=t, aes(w, x)) + geom_line()
Run Code Online (Sandbox Code Playgroud)
但这不是:
i <- 'x'
ggplot(data=t, aes(w, i)) + geom_line()
Run Code Online (Sandbox Code Playgroud)
如果我想最终遍历x,y和z,那么这是一个问题.有帮助吗?
我有一个dataframe
,我想应用一个取三列值的函数,并计算三个值之间的最小差异.
#dataset
df <- data.frame(a= sample(1:100, 10),b = sample(1:100, 10),c= sample(1:100, 10))
#function
minimum_distance <- function(a,b,c)
{
dist1 <- abs(a-b)
dist2 <- abs(a-c)
dist3 <- abs(b-c)
return(min(dist1,dist2,dist3))
}
Run Code Online (Sandbox Code Playgroud)
我正在寻找类似的东西:
df$distance <- sapply(df, function(x) minimum_distance(x$a,x$b,x$c) )
## errormessage
Error in x$a : $ operator is invalid for atomic vectors
Run Code Online (Sandbox Code Playgroud)
虽然我可以使用ddply:
df2 <- ddply(df,.(a),function(r) {data.frame(min_distance=minimum_distance(r$a,r$b, r$c))}, .drop=FALSE)
Run Code Online (Sandbox Code Playgroud)
这并没有保留所有列.有什么建议?
编辑:我最终使用:
df$distance <- mapply(minimum_distance, df$a, df$b, df$c)
Run Code Online (Sandbox Code Playgroud) ggplot2
在R 中使用库我最喜欢的一个方面是能够轻松指定美学.我可以快速制作一个散点图并应用与特定列关联的颜色,我希望能够使用python/pandas/matplotlib执行此操作.我想知道是否有任何便利功能,人们用它来使用pandas数据帧和Matplotlib将颜色映射到值?
##ggplot scatterplot example with R dataframe, `df`, colored by col3
ggplot(data = df, aes(x=col1, y=col2, color=col3)) + geom_point()
##ideal situation with pandas dataframe, 'df', where colors are chosen by col3
df.plot(x=col1,y=col2,color=col3)
Run Code Online (Sandbox Code Playgroud)
编辑:感谢您的回复,但我想包含一个示例数据框,以澄清我的要求.两列包含数字数据,第三列是分类变量.我想的脚本将根据此值指定颜色.
import pandas as pd
df = pd.DataFrame({'Height':np.random.normal(10),
'Weight':np.random.normal(10),
'Gender': ["Male","Male","Male","Male","Male",
"Female","Female","Female","Female","Female"]})
Run Code Online (Sandbox Code Playgroud) 我使用ggplot进行缩放时遇到了一个有趣的问题.我有一个数据集,我可以使用默认的线性刻度来很好地绘制,但是当我使用scale_y_log10()时,数字就会消失.这是一些示例代码和两张图片.请注意,线性标度中的最大值为~700,而日志缩放产生的值为10 ^ 8.我告诉你整个数据集只有大约8000个条目,所以有些东西是不对的.
我想这个问题与我的数据集结构和分箱有关,因为我不能在像'钻石'这样的常见数据集上复制这个错误.但是我不确定排除故障的最佳方法.
谢谢,zach cp
编辑:bdamarest可以重现钻石数据集上的比例问题,如下所示:
example_1 = ggplot(diamonds, aes(x=clarity, fill=cut)) +
geom_bar() + scale_y_log10(); print(example_1)
Run Code Online (Sandbox Code Playgroud)
#data.melt is the name of my dataset
> ggplot(data.melt, aes(name, fill= Library)) + geom_bar()
> ggplot(data.melt, aes(name, fill= Library)) + geom_bar() + scale_y_log10()
> length(data.melt$name)
[1] 8003
Run Code Online (Sandbox Code Playgroud)
这是一些示例数据......我想我看到了问题.原始熔化的数据集可能已经长约10 ^ 8行.也许行号被用于统计数据?
> head(data.melt)
Library name group
221938 AB Arthrofactin glycopeptide
235087 AB Putisolvin cyclic peptide
235090 AB Putisolvin cyclic peptide
222125 AB Arthrofactin glycopeptide
311468 AB Triostin cyclic depsipeptide
92249 AB CDA lipopeptide
> …
Run Code Online (Sandbox Code Playgroud) 我有一个使用RGL的3D图.我想用颜色制作相同的图来突出显示某些变量的分布.要做到这一点,我想有相同的情节,我如何找到和设置情节的方向?
一旦我做了一个初步的情节,我移动它找到一个很好的显示角度,我想保存这个角度,并将其纳入未来的绘图脚本.有人建议如何做到这一点?
library(rgl)
plot3d(iris)
#play with the plot to find a good angle
#save the angle for future plots
Run Code Online (Sandbox Code Playgroud) 我已经看到了iPython开发人员关于如何将ipython笔记本转换为博客文章,pdf,甚至整本书的一些谈话 (约43分钟).的PDF到X转换器解释其被写在降价或代码的IPython的细胞和在一个步骤中吐出新格式化的文档.
我的问题是我想生成一个大型文档,其中许多图形和部分是以编程方式生成的 - 就像这样.为了使用上面的方法在iPython中工作,我需要能够编写一个能编写其他iPython-Code-Blocks的函数.这种能力是否存在?
#some pseudocode to give an idea
for variable in list:
image = make_image(variable)
write_iPython_Markdown_Cell(variable)
write_iPython_Image_cell(image)
Run Code Online (Sandbox Code Playgroud)
我认为这可能有用,所以我想知道是否:
谢谢,zach cp
编辑:根据托马斯的建议,我在ipython邮件列表上发布了一些关于这个想法的可行性的反馈.简而言之 - 有一些技术上的困难使得这个想法不太适合原始想法.对于您想要生成markdown -cells和相应图像/表的重复报告,通过ipython内核/浏览器工作比直接使用像Jinja这样的模板系统生成报告要复杂得多.