首页 \ 问答 \ 如何将pandas数据帧转换为一维数组?(How to convert a pandas dataframe into one dimensional array?)

如何将pandas数据帧转换为一维数组?(How to convert a pandas dataframe into one dimensional array?)

我有一个数据帧X 我想将它转换为只有5个元素的1D数组。 一种方法是将内部数组转换为列表。 我怎样才能做到这一点?

      0     1   2          3           4           5
0   1622    95  1717   85.278544    1138.964373 1053.685830
1   62     328  390    75.613900    722.588235  646.974336
2   102    708  810    75.613900    800.916667  725.302767
3   102    862  964    75.613900    725.870370  650.256471
4   129    1380 1509   75.613900    783.711111  708.097211

val = X.values将给出一个numpy数组。 我想将数组的内部元素转换为列表。 我怎样才能做到这一点? 我尝试了这个但失败了

M = val.values.tolist()
A = np.array(M,dtype=list)
N = np.array(M,dtype=object)

I have a dataframe X. I want to convert it into 1D array with only 5 elements. One way of doing it is converting the inner arrays to lists. How can I do that?

      0     1   2          3           4           5
0   1622    95  1717   85.278544    1138.964373 1053.685830
1   62     328  390    75.613900    722.588235  646.974336
2   102    708  810    75.613900    800.916667  725.302767
3   102    862  964    75.613900    725.870370  650.256471
4   129    1380 1509   75.613900    783.711111  708.097211

val = X.values will give a numpy array. I want to convert the inner elements of the array to list. How can I do that? I tried this but failed

M = val.values.tolist()
A = np.array(M,dtype=list)
N = np.array(M,dtype=object)

原文:https://stackoverflow.com/questions/45099354
更新时间:2021-12-05 18:12

最满意答案

我认为这里发生的事情是第一次调用ShowModal()阻止应用程序级别(而不仅仅是帧级别)阻止第二个对话框完全初始化。 要解决此问题,我将调用Show()而不是ShowModal()并将wx.FRAME_FLOAT_ON_PARENT添加到对话框样式标志。 您还可以在对话框打开时对您不希望用户进行交互的程序部分调用Disable()

编辑:这是一个工作的例子:

from threading import Thread
from time import sleep
import wx


class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title="TEST", size=(400, 400))
        self.Show()
        self.__someDialog = None
        self.__myThread = None

        self.__okButton = wx.Button(self, -1, "Press me")
        self.Bind(wx.EVT_BUTTON, self.__onOK)

        self.__myThread = Thread(target=self.__waitThenClose, name="Closer")
        self.__myThread.setDaemon(True)
        self.__myThread.start()

    def __onOK(self, evt):
        self.__someDialog = SomeDialog(self)
        self.__someDialog.ShowModal()

    def closeOpenDialogs(self, evt=None):
        lst = wx.GetTopLevelWindows()

        for i in range(len(lst) - 1, 0, -1):
            dialog = lst[i]
            if isinstance(dialog, wx.Dialog):
                print "Closing " + str(dialog)
                # dialog.Close(True)
                wx.CallAfter(dialog.Close)
                # sleep(1)
                # dialog.Destroy()

    def __waitThenClose(self):

        for x in range(0, 10):
            print "Sleeping..."
            sleep(1)

        wx.CallAfter(self.closeOpenDialogs)
        wx.CallAfter(self.Close, True)


class SomeDialog(wx.Dialog):
    def __init__(self, parent):
        wx.Dialog.__init__(self, parent, id=-1, title='Some Dialog')
        self.SetSize((300, 300))
        self.__anotherDialog = None
        self.__okButton = wx.Button(self, -1, "Press me")

        self.Bind(wx.EVT_BUTTON, self.__onOK)
        wx.EVT_CLOSE(self, self.__on_btn_cancel)

    def __onOK(self, evt):
        self.__anotherDialog = AnotherDialog(self)
        self.__anotherDialog.SetWindowStyleFlag(wx.FRAME_FLOAT_ON_PARENT|wx.DEFAULT_DIALOG_STYLE)
        self.__anotherDialog.Show()

    def __on_btn_cancel(self, event):
        event.Skip()
        self.EndModal(wx.ID_CANCEL)


class AnotherDialog(wx.Dialog):
    def __init__(self, parent):
        wx.Dialog.__init__(self, parent, id=-1, title='Another Dialog')
        self.SetSize((200, 200))
        wx.EVT_CLOSE(self, self.__on_btn_cancel)
        parent.Disable()

    def __on_btn_cancel(self, event):
        event.Skip()
        self.GetParent().Enable()
        # self.EndModal(wx.ID_CANCEL)


if __name__ == "__main__":

    app = wx.App()

    mainFrame = MainFrame()

    app.MainLoop()

I think what is happening here is that the first call to ShowModal() blocks the at the app level (not just the frame level) which is preventing the second dialog from becoming fully initialized. To work around this issue I would call Show() instead of ShowModal() and add wx.FRAME_FLOAT_ON_PARENT to the dialog style flags. You can also call Disable() on the parts of the program you don't want the user to interact with while the dialogs are open.

EDIT: Here is a working example:

from threading import Thread
from time import sleep
import wx


class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title="TEST", size=(400, 400))
        self.Show()
        self.__someDialog = None
        self.__myThread = None

        self.__okButton = wx.Button(self, -1, "Press me")
        self.Bind(wx.EVT_BUTTON, self.__onOK)

        self.__myThread = Thread(target=self.__waitThenClose, name="Closer")
        self.__myThread.setDaemon(True)
        self.__myThread.start()

    def __onOK(self, evt):
        self.__someDialog = SomeDialog(self)
        self.__someDialog.ShowModal()

    def closeOpenDialogs(self, evt=None):
        lst = wx.GetTopLevelWindows()

        for i in range(len(lst) - 1, 0, -1):
            dialog = lst[i]
            if isinstance(dialog, wx.Dialog):
                print "Closing " + str(dialog)
                # dialog.Close(True)
                wx.CallAfter(dialog.Close)
                # sleep(1)
                # dialog.Destroy()

    def __waitThenClose(self):

        for x in range(0, 10):
            print "Sleeping..."
            sleep(1)

        wx.CallAfter(self.closeOpenDialogs)
        wx.CallAfter(self.Close, True)


class SomeDialog(wx.Dialog):
    def __init__(self, parent):
        wx.Dialog.__init__(self, parent, id=-1, title='Some Dialog')
        self.SetSize((300, 300))
        self.__anotherDialog = None
        self.__okButton = wx.Button(self, -1, "Press me")

        self.Bind(wx.EVT_BUTTON, self.__onOK)
        wx.EVT_CLOSE(self, self.__on_btn_cancel)

    def __onOK(self, evt):
        self.__anotherDialog = AnotherDialog(self)
        self.__anotherDialog.SetWindowStyleFlag(wx.FRAME_FLOAT_ON_PARENT|wx.DEFAULT_DIALOG_STYLE)
        self.__anotherDialog.Show()

    def __on_btn_cancel(self, event):
        event.Skip()
        self.EndModal(wx.ID_CANCEL)


class AnotherDialog(wx.Dialog):
    def __init__(self, parent):
        wx.Dialog.__init__(self, parent, id=-1, title='Another Dialog')
        self.SetSize((200, 200))
        wx.EVT_CLOSE(self, self.__on_btn_cancel)
        parent.Disable()

    def __on_btn_cancel(self, event):
        event.Skip()
        self.GetParent().Enable()
        # self.EndModal(wx.ID_CANCEL)


if __name__ == "__main__":

    app = wx.App()

    mainFrame = MainFrame()

    app.MainLoop()

相关问答

更多
  • 从这个类似的问题我发现: Basta计算的Buzof 这是诀窍。 还有一款名为DialogDevil的产品,它看起来很有前途,但由于某种原因在我们的情况下无法使用。 From this similar question I found: Buzof by Basta Computing which did the trick. There is also a product called DialogDevil which looked promising but didn't work in our si ...
  • 基本上,如果某些东西不是声明式的,那么你有一个指令 。 .directive('shadow', function() { return { scope: { target: '=shadow' }, link: function(scope, el, att) { scope[att.shadow] = angular.copy(scope.target); scope.commit = function() { ...
  • 这就是我提出的,因为这显然是模态对话框的一个错误,我可以向你展示一个可行的“黑客”,但我认为它弄乱的原因是当你创建一个模态时它添加了对话框
    在对话框div之上,并且因为你将所有对话框直接附加到正文,所以在一段时间之后需要关闭哪些对象会混淆(这只是我的假设,我真的不应该这样做):) 解决方法是每次调用CreateDalog时检查对话框的数量和叠加的数量,如果它们不匹配,您手动插入一个新的叠加层,这将解决您的问题。 有一点是,由于这个 ...
  • 这主要是基于意见的,但我会尝试提出一些论点: 在解决方案1中,模态对话框突然消失。 但模态对话框的基本原理是暂停UI,直到用户完成某个操作。 因此恕我直言,如果没有其他消息突然消失,会给用户带来不一致的体验 解决方案2实现一致且简单(且稳健)。 唯一的问题是用户可能会觉得应用程序可能已经注意到网络再次出现,并且可能会因此而责怪懒惰的开发人员。 如果您只更换消息,解决方案3看起来很愚蠢网络已关闭 , 网络已 关闭 。 如果用户停止在屏幕上看一会儿(喝咖啡,跟同事说话)并且网络在此期间停机然后上升,当他再次看屏 ...
  • 您应该能够捕获wx.CLOSE事件并阻止关闭对话框。 但是,我相信你最好使用向导式方法。 在第一步中,您将检查数字,然后单击“下一步”。 然后你会接受名称并关闭对话框。 我觉得这对用户来说是更自然的流程。 我把一个有趣的小例子放在一起,但你会注意到我没有以任何有意义的方式保存数据。 你想要自己添加: import wx ######################################################################## class MyDialog(wx.Dia ...
  • 在您的OnPaint中,请致电: e.Skip() 在您的测试中,请致电: dial.ShowModal() 而不是dial.Show(),除非你真的想要显示一个无模式的消息对话框,我打赌你没有。 您还应该将父级设置为MessageDialog。 在这种情况下它应该是self(意味着Paint对象)。 In your OnPaint, call: e.Skip() In your Test, call: dial.ShowModal() instead of dial.Show(), unless ...
  • 我想你可能不得不为这个使用一个自定义的wx.Dialog 。 您可以使用wx.FutureCall在将来调用触发事件。 就像是: class MessageDialog(wx.Dialog): def __init__(self, message, title): wx.Dialog.__init__(self, None, -1, title,size=(300, 120)) self.CenterOnScreen(wx.BOTH) ok = wx ...
  • 你可以调用window.top.dialogClose(); 这应该够了吧。 You can call window.top.dialogClose(); That should do the trick.
  • 我认为这里发生的事情是第一次调用ShowModal()阻止应用程序级别(而不仅仅是帧级别)阻止第二个对话框完全初始化。 要解决此问题,我将调用Show()而不是ShowModal()并将wx.FRAME_FLOAT_ON_PARENT添加到对话框样式标志。 您还可以在对话框打开时对您不希望用户进行交互的程序部分调用Disable() 。 编辑:这是一个工作的例子: from threading import Thread from time import sleep import wx class Ma ...
  • 因为我真的不明白你的要求,我会改为说“我希望wxpython只有一个closeAll():)” def wx_closeAll(): for win in wx.GetTopLevelWindows(): win.Destroy() def closeAllButPrimary(): for win in wx.GetTopLevelWindows(): if win != wx.GetTopLevelWindow(): win.Des ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)