当我尝试按行合并两个数据帧时:
bigdata = data1.append(data2)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Run Code Online (Sandbox Code Playgroud)Exception: Index cannot contain duplicate values!
第一个数据帧的索引从0到38开始,第二个从0到48开始.我不明白我必须在合并之前修改其中一个数据帧的索引,但我不知道如何.
谢谢.
这是两个数据帧:
data1:
meta particle ratio area type
0 2 part10 1.348 0.8365 touching
1 2 part18 1.558 0.8244 single
2 2 part2 1.893 0.894 single
3 2 part37 0.6695 1.005 single
....clip...
36 2 part23 1.051 0.8781 single
37 2 part3 80.54 0.9714 nuclei
38 2 part34 1.071 0.9337 single
Run Code Online (Sandbox Code Playgroud)
data2:
meta particle ratio area type
0 3 part10 0.4756 1.025 single
1 3 part18 …Run Code Online (Sandbox Code Playgroud) 我们有两个列表,A和B:
A = ['a','b','c']
B = [1, 2]
Run Code Online (Sandbox Code Playgroud)
是否有一种pythonic方法来构建包含2 ^ n的A和B之间的所有映射集(此处为2 ^ 3 = 8)?那是:
[(a,1), (b,1), (c,1)]
[(a,1), (b,1), (c,2)]
[(a,1), (b,2), (c,1)]
[(a,1), (b,2), (c,2)]
[(a,2), (b,1), (c,1)]
[(a,2), (b,1), (c,2)]
[(a,2), (b,2), (c,1)]
[(a,2), (b,2), (c,2)]
Run Code Online (Sandbox Code Playgroud)
使用itertools.product,可以获得所有元组:
import itertools as it
P = it.product(A, B)
[p for p in P]
Run Code Online (Sandbox Code Playgroud)
这使:
Out[3]: [('a', 1), ('a', 2), ('b', 1), ('b', 2), ('c', 1), ('c', 2)]
Run Code Online (Sandbox Code Playgroud) 让一个数组:
a =np.array([[1,2],[3,-5],[6,-15],[10,7]])
Run Code Online (Sandbox Code Playgroud)
为了获得第二列元素高于-6的行,可以这样做
>>> a[a[:,1]>-6]
array([[ 1, 2],
[ 3, -5],
[10, 7]])
Run Code Online (Sandbox Code Playgroud)
但如何在-6; 3之间获得第二个元素的线条?我试过了:
>>> a[3>a[:,1]>-6]
Run Code Online (Sandbox Code Playgroud)
而且(引起错误):
>>> np.ma.masked_inside(a,-6,3)
Run Code Online (Sandbox Code Playgroud)
这使:
masked_array(data =
[[-- --]
[-- --]
[6 -15]
[10 7]],
mask =
[[ True True]
[ True True]
[False False]
[False False]],
fill_value = 999999)
Run Code Online (Sandbox Code Playgroud)
但结果不太清楚
谢谢jp
我正在尝试做一些与上一个问题非常相似的事情,但我遇到了错误.我有一个包含功能和标签的pandas数据框我需要进行一些转换以将功能和标签变量发送到机器学习对象:
import pandas
import milk
from scikits.statsmodels.tools import categorical
Run Code Online (Sandbox Code Playgroud)
然后我有:
trainedData=bigdata[bigdata['meta']<15]
untrained=bigdata[bigdata['meta']>=15]
#print trainedData
#extract two columns from trainedData
#convert to numpy array
features=trainedData.ix[:,['ratio','area']].as_matrix(['ratio','area'])
un_features=untrained.ix[:,['ratio','area']].as_matrix(['ratio','area'])
print 'features'
print features[:5]
##label is a string:single, touching,nuclei,dust
print 'labels'
labels=trainedData.ix[:,['type']].as_matrix(['type'])
print labels[:5]
#convert single to 0, touching to 1, nuclei to 2, dusts to 3
#
tmp=categorical(labels,drop=True)
targets=categorical(labels,drop=True).argmax(1)
print targets
Run Code Online (Sandbox Code Playgroud)
输出控制台首先产生:
features
[[ 0.38846334 0.97681855]
[ 3.8318634 0.5724734 ]
[ 0.67710876 1.01816444]
[ 1.12024943 0.91508699]
[ 7.51749674 1.00156707]]
labels
[[single]
[touching] …Run Code Online (Sandbox Code Playgroud) 从ubuntu 10.04开始,我用easy_install安装了pylab.经过一些升级,我可以导入pylab.首先,我从easy_install运行ipython:
$ ipython
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
Type "copyright", "credits" or "license" for more information.
IPython 0.11 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
Run Code Online (Sandbox Code Playgroud)
然后我尝试导入matplotlib
In [1]: import matplotlib
In [2]: matplotlib.__version__
Out[2]: '1.0.1'
Run Code Online (Sandbox Code Playgroud)
但是当导入pylab failes时如下:
In [3]: import matplotlib.pylab
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
/home/claire/<ipython-input-3-1d30b9aee20b> in <module>() …Run Code Online (Sandbox Code Playgroud) 我有一个名为projectxml的etree对象:
projetxml type <type 'lxml.etree._Element'>
Run Code Online (Sandbox Code Playgroud)
我需要将它保存在磁盘上,所以我将其转换为元素树:
savedxml=et.ElementTree(projetxml)
savedxml.write('/home/simon/Vysis.xml')
Run Code Online (Sandbox Code Playgroud)
另一个脚本必须加载Vysis.xml和另外两个相同类型的文件:
vysis=et.parse('/home/simon/Vysis.xml')
asi=et.parse('/home/simon/ASI.xml')
psi=et.parse('/home/simon/PSI.xml')
Run Code Online (Sandbox Code Playgroud)
现在asi,psi和vysis lxml对象的类型例如:
<lxml.etree._ElementTree object at 0xa7eaf8c>
Run Code Online (Sandbox Code Playgroud)
我的问题是我不能再做了:
R=et.Element('DataBase')
R.append(asi)
R.append(psi)
R.append(vysis)
Run Code Online (Sandbox Code Playgroud)
因为错误:
R.append(asi)
File "lxml.etree.pyx", line 697, in lxml.etree._Element.append (src/lxml /lxml.etree.c:35471)
TypeError: Argument 'element' has incorrect type (expected lxml.etree._Element, got lxml.etree._ElementTree)
Run Code Online (Sandbox Code Playgroud)
我想我有两个解决方案.第一个可能是避免将etree.Element转换为etree.ElementTree并将其"直接"保存,但我不知道如何.第二个解决办法是转换回etree.ElementTree到etree.Element类型......应该有一个清晰的解决方案,以保存/载入一个XML对象?
如何转换元组:
t=(('1','a'), ('1','A'), ('2','b'), ('2','B'), ('3','c'),('3', 'C'))
Run Code Online (Sandbox Code Playgroud)
进入字典:
{'1':('a','A'),'2':('b','B'),'3':('c','C')}
Run Code Online (Sandbox Code Playgroud)
我试过在控制台:
>>> d={}
>>> t[0]
(1, 'a')
>>> d[t[0][0]]=t[0][1]
>>> d
{1: 'a'}
>>> t[0][0] in d
True
>>> d[t[1][0]]=t[1][1]
>>> d
{1: 'A'}
>>> d[t[0][0]]=t[0][1]
>>> d[t[1][0]]=d[t[1][0]],t[1][1]
>>> d
{1: ('a', 'A')}
Run Code Online (Sandbox Code Playgroud)
现在,以下脚本无法完成此任务:
t=(('1','a'), ('1','A'), ('2','b'), ('2','B'), ('3','c'),('3', 'C'))
print "{'1':('a','A'),'2':('b','B'),'3':('c','C')} wanted, not:",dict(t)
d={}
for c, ob in enumerate(t):
print c,ob[0], ob[1]
if ob[0] in d:
print 'test'
d[ob[0]]=d[ob[0]],ob[1]
print d
else:
print 'else', d, ob[0],ob[1]
d[ob[0]]=d[ob[1]] # Errror is …Run Code Online (Sandbox Code Playgroud) 我喜欢在Python脚本中转换以下字符串:
mystring='(5,650),(235,650),(465,650),(695,650)'
Run Code Online (Sandbox Code Playgroud)
到元组列表
mytuple=[(5,650),(235,650),(465,650),(695,650)]
Run Code Online (Sandbox Code Playgroud)
这样
print mytuple[0]得到:
(5,650)
Run Code Online (Sandbox Code Playgroud) 目的是将纯红色图像转换为色轮的任何色调.

问题是只能获得绿色或蓝色图像(例如黄色,角度约为30°):

在一些ipython单元格中执行的代码依赖于scikit-image 0.10dev:
from skimage import io
from skimage import color
from scipy import ndimage as nd
import numpy as np
from matplotlib import pyplot as plt
import os
cy55 = io.imread('/home/jeanpat/MFISH/PSI/P07/01/Cy5/P070109C.tif')
zero = np.zeros(cy55.shape,dtype=np.uint8)
rgb0 = np.dstack([cy55, zero,zero])
hue_rotations = [18, 36,72,90,108]
images = {}
images[0] = rgb0
hsv0 = color.rgb2hsv(rgb0)
print hsv0[:,:,0].dtype
for hue in hue_rotations:
hsv = np.copy(hsv0)
hsv[:,:,0] = hsv[:,:,0]+ hue
rgb = color.hsv2rgb(hsv)
images[hue] = rgb
i = …Run Code Online (Sandbox Code Playgroud) python ×8
numpy ×2
pandas ×2
colors ×1
configparser ×1
dictionary ×1
hsv ×1
list ×1
lxml ×1
matplotlib ×1
range ×1
scikit-image ×1
tuples ×1
ubuntu ×1