<legend id='Wctxz'><style id='Wctxz'><dir id='Wctxz'><q id='Wctxz'></q></dir></style></legend>
  1. <small id='Wctxz'></small><noframes id='Wctxz'>

      <bdo id='Wctxz'></bdo><ul id='Wctxz'></ul>
  2. <i id='Wctxz'><tr id='Wctxz'><dt id='Wctxz'><q id='Wctxz'><span id='Wctxz'><b id='Wctxz'><form id='Wctxz'><ins id='Wctxz'></ins><ul id='Wctxz'></ul><sub id='Wctxz'></sub></form><legend id='Wctxz'></legend><bdo id='Wctxz'><pre id='Wctxz'><center id='Wctxz'></center></pre></bdo></b><th id='Wctxz'></th></span></q></dt></tr></i><div id='Wctxz'><tfoot id='Wctxz'></tfoot><dl id='Wctxz'><fieldset id='Wctxz'></fieldset></dl></div>
    1. <tfoot id='Wctxz'></tfoot>

      将一列字符串转换为 pandas 列表

      Convert a columns of string to list in pandas(将一列字符串转换为 pandas 列表)
      • <i id='oazAP'><tr id='oazAP'><dt id='oazAP'><q id='oazAP'><span id='oazAP'><b id='oazAP'><form id='oazAP'><ins id='oazAP'></ins><ul id='oazAP'></ul><sub id='oazAP'></sub></form><legend id='oazAP'></legend><bdo id='oazAP'><pre id='oazAP'><center id='oazAP'></center></pre></bdo></b><th id='oazAP'></th></span></q></dt></tr></i><div id='oazAP'><tfoot id='oazAP'></tfoot><dl id='oazAP'><fieldset id='oazAP'></fieldset></dl></div>
        <tfoot id='oazAP'></tfoot>
        1. <small id='oazAP'></small><noframes id='oazAP'>

          <legend id='oazAP'><style id='oazAP'><dir id='oazAP'><q id='oazAP'></q></dir></style></legend>
              <tbody id='oazAP'></tbody>
            • <bdo id='oazAP'></bdo><ul id='oazAP'></ul>

              1. 本文介绍了将一列字符串转换为 pandas 列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我对 pandas 数据框中的一列的类型有疑问.基本上,该列作为字符串保存在 csv 文件中,我想将其用作元组以便能够将其转换为数字列表.下面是一个非常简单的csv:

                I have a problem with the type of one of my column in a pandas dataframe. Basically the column is saved in a csv file as a string, and I wanna use it as a tuple to be able to convert it in a list of numbers. Following there is a very simple csv:

                ID,LABELS
                1,"(1.0,2.0,2.0,3.0,3.0,1.0,4.0)"
                2,"(1.0,2.0,2.0,3.0,3.0,1.0,4.0)"
                

                如果使用read_csv"函数加载它,我会得到一个字符串列表.我试图转换为列表,但我得到了字符串的列表版本:

                If a load it with the function "read_csv" I get a list of strings. I have tried to convert to a list, but I get the list version of a string:

                df.LABELS.apply(lambda x: list(x))
                

                返回:

                ['(','1','.','0',.,.,.,.,.,'4','.','0',')']
                

                你知道怎么做吗?

                谢谢.

                推荐答案

                你可以使用 ast.literal_eval,它会给你一个元组:

                You can use ast.literal_eval, which will give you a tuple:

                import ast
                df.LABELS = df.LABELS.apply(ast.literal_eval)
                

                如果您确实想要一个列表,请使用:

                If you do want a list, use:

                df.LABELS.apply(lambda s: list(ast.literal_eval(s)))
                

                这篇关于将一列字符串转换为 pandas 列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                How to extend Python class init(如何扩展 Python 类初始化)
                What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                Initialize list with same bool value(使用相同的布尔值初始化列表)
                setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)
                <i id='I1cRl'><tr id='I1cRl'><dt id='I1cRl'><q id='I1cRl'><span id='I1cRl'><b id='I1cRl'><form id='I1cRl'><ins id='I1cRl'></ins><ul id='I1cRl'></ul><sub id='I1cRl'></sub></form><legend id='I1cRl'></legend><bdo id='I1cRl'><pre id='I1cRl'><center id='I1cRl'></center></pre></bdo></b><th id='I1cRl'></th></span></q></dt></tr></i><div id='I1cRl'><tfoot id='I1cRl'></tfoot><dl id='I1cRl'><fieldset id='I1cRl'></fieldset></dl></div>
                  • <legend id='I1cRl'><style id='I1cRl'><dir id='I1cRl'><q id='I1cRl'></q></dir></style></legend>

                        <tbody id='I1cRl'></tbody>
                      <tfoot id='I1cRl'></tfoot>

                        <small id='I1cRl'></small><noframes id='I1cRl'>

                          <bdo id='I1cRl'></bdo><ul id='I1cRl'></ul>