从数据框中删除重复列的最简单方法是什么?
我正在通过以下方式阅读具有重复列的文本文件:
import pandas as pd
df=pd.read_table(fname)
Run Code Online (Sandbox Code Playgroud)
列名是:
Time, Time Relative, N2, Time, Time Relative, H2, etc...
Run Code Online (Sandbox Code Playgroud)
所有时间和时间相对列包含相同的数据.我想要:
Time, Time Relative, N2, H2
Run Code Online (Sandbox Code Playgroud)
我所有的删除,删除等尝试,例如:
df=df.T.drop_duplicates().T
Run Code Online (Sandbox Code Playgroud)
导致唯一值索引错误:
Reindexing only valid with uniquely valued index objects
Run Code Online (Sandbox Code Playgroud)
很抱歉是熊猫菜鸟.任何建议,将不胜感激.
额外细节
Pandas版本:0.9.0
Python版本:2.7.3
Windows 7
(通过Pythonxy 2.7.3.0安装)
数据文件(注意:在真实文件中,列由制表符分隔,这里它们用4个空格分隔):
Time Time Relative [s] N2[%] Time Time Relative [s] H2[ppm]
2/12/2013 9:20:55 AM 6.177 9.99268e+001 2/12/2013 9:20:55 AM 6.177 3.216293e-005
2/12/2013 9:21:06 AM 17.689 9.99296e+001 2/12/2013 9:21:06 AM 17.689 3.841667e-005
2/12/2013 9:21:18 AM 29.186 9.992954e+001 2/12/2013 9:21:18 …
Run Code Online (Sandbox Code Playgroud) 有没有人知道是否已经有一个小部件/类来处理基于tkinter/ttk中的切换按钮(checkbutton)扩展/收缩框架?
这个问题源于我试图清理一个混乱的gui,这个gui有很多通过特定操作分类的选项.我想要的是:
在谷歌上找到的例子
然而,不仅仅是文本,允许按钮,条目,任何tkinter的小部件.如果这不存在,那么创建一个继承tkinter框架的类是否可行/有用:
import tkinter as tk
import ttk
class toggledFrame(tk.Frame):
def __init__(self):
self.show=tk.IntVar()
self.show.set(0)
self.toggleButton=tk.Checkbutton(self, command=self.toggle, variable=self.show)
self.toggleButton.pack()
self.subFrame=tk.Frame(self)
def toggle(self):
if bool(self.show.get()):
self.subFrame.pack()
else:
self.subFrame.forget()
Run Code Online (Sandbox Code Playgroud)
注意:此代码未经测试,仅提供概念
我正在尝试构建一个python类QGraphicsRectItem
(PySide或PyQt4),它通过悬停提供鼠标交互,可移动,并且可以重新调整大小.我几乎一切都在工作,除了:
出于某种原因,当重新调整或移动项目时,似乎鼠标悬停区域没有变化.我需要帮助解决这个问题.
也许问题是由于反转y轴引起的QGraphicsView
:
QGraphicsView.scale(1,-1)
Run Code Online (Sandbox Code Playgroud)
class BoxResizable(QtGui.QGraphicsRectItem):
def __init__(self, rect, parent = None, scene = None):
QtGui.QGraphicsRectItem.__init__(self, rect, parent, scene)
self.setZValue(1000)
self._rect = rect
self._scene = scene
self.mouseOver = False
self.resizeHandleSize = 4.0
self.mousePressPos = None
self.mouseMovePos = None
self.mouseIsPressed = False
self.setFlags(QtGui.QGraphicsItem.ItemIsSelectable|QtGui.QGraphicsItem.ItemIsFocusable)
self.setAcceptsHoverEvents(True)
self.updateResizeHandles()
def hoverEnterEvent(self, event):
self.updateResizeHandles()
self.mouseOver = True
self.prepareGeometryChange()
def hoverLeaveEvent(self, event):
self.mouseOver = False
self.prepareGeometryChange()
def hoverMoveEvent(self, event):
if self.topLeft.contains(event.scenePos()) or self.bottomRight.contains(event.scenePos()):
self.setCursor(QtCore.Qt.SizeFDiagCursor)
elif self.topRight.contains(event.scenePos()) or self.bottomLeft.contains(event.scenePos()):
self.setCursor(QtCore.Qt.SizeBDiagCursor)
else:
self.setCursor(QtCore.Qt.SizeAllCursor)
QtGui.QGraphicsRectItem.hoverMoveEvent(self, …
Run Code Online (Sandbox Code Playgroud) 这个特定问题源于尝试处理由 MATLAB 算法生成的大型数据集,以便我可以使用 python 算法处理它们。
背景:我在 MATLAB 中有大型数组(通常为 20x20x40x15000 [i,j,k,frame]),我想在 python 中使用它们。所以我将数组保存到一个 *.mat 文件中,并用于scipy.io.loadmat(fname)
将 *.mat 文件读入一个 numpy 数组。但是,出现了一个问题,如果我尝试在 python 中加载整个 *.mat 文件,则会发生内存错误。为了解决这个问题,我将 *.mat 文件切成小块,这样我就可以一次将一个小块加载到 python 数组中。如果我按帧划分 *.mat,我现在有 15,000 个 *.mat 文件,这些文件很快就会变得很麻烦(至少在 Windows 中)。所以我的解决方案是使用压缩文件。
问题:我可以使用 scipy 直接从压缩文件中读取 *.mat 文件,而无需先将文件解压缩到当前工作目录吗?
规格: Python 2.7,Windows XP
当前代码:
import scipy.io
import zipfile
import numpy as np
def readZip(zfilename,dim,frames):
data=np.zeros((dim[0],dim[1],dim[2],frames),dtype=np.float32)
zfile = zipfile.ZipFile( zfilename, "r" )
i=0
for info in zfile.infolist():
fname = info.filename
zfile.extract(fname)
mat=scipy.io.loadmat(fname)
data[:,:,:,i]=mat['export']
mat.clear()
i=i+1
return data
Run Code Online (Sandbox Code Playgroud)
试过的代码: …
有谁知道在tkinter(python)中是否可以使用带有两个“滑块”的单个比例小部件?还是反正要假的呢?
如(用gimp编辑):
我正在考虑将一个秤放在另一个秤上,但是背景透明?
首先,我在windows xp机器上使用python [2.7.2],numpy [1.6.2rc1],cython [0.16],gcc [MinGW]编译器.
我需要一个3D连通分量算法来处理存储在numpy数组中的一些3D二进制数据(即1和0).不幸的是,我找不到任何现有的代码,所以我改编了这里的代码来处理3D数组.一切都很好,但是处理大量数据集的速度是可取的.结果我偶然发现了cython,并决定尝试一下.
到目前为止,cython已经提高了速度:Cython:0.339 s Python:0.635 s
使用cProfile,我在纯python版本中的耗时行是:
new_region = min(filter(lambda i: i > 0, array_region[xMin:xMax,yMin:yMax,zMin:zMax].ravel()))
Run Code Online (Sandbox Code Playgroud)
问题: "cythonize"线路的正确方法是什么:
new_region = min(filter(lambda i: i > 0, array_region[xMin:xMax,yMin:yMax,zMin:zMax].ravel()))
for x,y,z in zip(ind[0],ind[1],ind[2]):
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激,希望这项工作将有助于其他人.
纯python版本[*.py]:
import numpy as np
def find_regions_3D(Array):
x_dim=np.size(Array,0)
y_dim=np.size(Array,1)
z_dim=np.size(Array,2)
regions = {}
array_region = np.zeros((x_dim,y_dim,z_dim),)
equivalences = {}
n_regions = 0
#first pass. find regions.
ind=np.where(Array==1)
for x,y,z in zip(ind[0],ind[1],ind[2]):
# get the region number from all surrounding cells including diagnols (27) …
Run Code Online (Sandbox Code Playgroud) 我想提取单芯片光学鼠标传感器(特别是 ADNS-2700)捕获的实际图像。与互联网上使用微控制器与成像芯片的 SPI 接口通信的各种其他教程(如下所示)不同,我尝试使用的芯片集成了 USB 接口。
我已经成功提取了以下示例中的按钮按下、速度和滚轮:
import usb.core
import usb.util
VENDOR_ID = 6447
PRODUCT_ID = 2326
# find the USB device
device = usb.core.find(idVendor=VENDOR_ID,
idProduct=PRODUCT_ID)
# use the first/default configuration
device.set_configuration()
# first endpoint
endpoint = device[0][(0,0)][0]
# read a data packet
attempts = 10
data = None
while attempts > 0:
try:
data = device.read(endpoint.bEndpointAddress,
endpoint.wMaxPacketSize)
print data
except usb.core.USBError as e:
data = None
if e.args == ('Operation timed …
Run Code Online (Sandbox Code Playgroud) 我在python 2.7和tkinter中编写了一个应用程序.我创建了一个带有几个按钮的工具栏,这些按钮打开了各自显示各种选项的顶部窗口.我使用ttk.Checkbutton以'toolbutton'样式作为指示器来显示选项窗口是打开还是关闭.
问题是如果选择了另一个窗口,选项窗口将返回到后面.目前,如果再次选择工具按钮,则选项窗口将关闭.但是,如果窗口位于顶部,我只想关闭窗口.如果选项窗口不在顶部,我希望窗口移到前面.
我工作的一些代码:
class MainWindow:
def __init__(self,application):
self.mainframe=tk.Frame(application)
application.geometry("900x600+30+30")
self.otherOptionsSelect=tk.IntVar()
self.otherOptions_Button=ttk.Checkbutton(application,style='Toolbutton',variable=self.otherOptionsSelect,
onvalue=1, offvalue=0,image=self.optionsIcon, command=self.otherOptions)
def otherOptions(self):
if self.otherOptionsSelect.get()==0:
self.otherOptions.destroy()
return
self.otherOptions=tk.Toplevel()
self.otherOptions.title("IsoSurface Options")
self.otherOptions.geometry("200x165+"+str(int(application.winfo_x())+555)+"+"+str(int(application.winfo_y())+230))
self.otherOptApply_button=ttk.Button(self.otherOptions,text="Apply",command=self.showFrame)
self.otherOptApply_button.place(x=20,y=80,width=50,height=30)
self.otherOptClose_button=ttk.Button(self.otherOptions,text="Close",command=self.otherOptionsClose)
self.otherOptClose_button.place(x=80,y=80,width=50,height=30)
def otherOptionsClose(self):
self.otherOptionsSelect.set(0)
self.otherOptions.destroy()
Run Code Online (Sandbox Code Playgroud)
这是我写的整个应用程序的图片:
在上图中,每个窗口都有各自的ttk.checkbutton.此时,切换按钮会打开或关闭窗口.但是,如果窗口位于应用程序前面,我真正想要它关闭窗口,或者如果窗口位于应用程序后面,则将窗口置于前面.
希望这能解决一些问题.
提前致谢!
我有相当大的4D阵列[20x20x40x15000],我使用h5py将其保存为磁盘作为HDF5文件.现在问题是我想计算整个数组的平均值,即使用:
numpy.average(HDF5_file)
Run Code Online (Sandbox Code Playgroud)
我得到了MemoryError
.似乎numpy尝试将HDF5文件加载到内存中以执行平均值?
有没有人有这个问题的优雅和有效的解决方案?