• <small id='Rj2yK'></small><noframes id='Rj2yK'>

    <legend id='Rj2yK'><style id='Rj2yK'><dir id='Rj2yK'><q id='Rj2yK'></q></dir></style></legend>

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

        使用 pandas 从 csv 文件中读回元组

        Reading back tuples from a csv file with pandas(使用 pandas 从 csv 文件中读回元组)
              <bdo id='7vGAe'></bdo><ul id='7vGAe'></ul>
            • <small id='7vGAe'></small><noframes id='7vGAe'>

                <tbody id='7vGAe'></tbody>

              <tfoot id='7vGAe'></tfoot>

                <legend id='7vGAe'><style id='7vGAe'><dir id='7vGAe'><q id='7vGAe'></q></dir></style></legend>

              • <i id='7vGAe'><tr id='7vGAe'><dt id='7vGAe'><q id='7vGAe'><span id='7vGAe'><b id='7vGAe'><form id='7vGAe'><ins id='7vGAe'></ins><ul id='7vGAe'></ul><sub id='7vGAe'></sub></form><legend id='7vGAe'></legend><bdo id='7vGAe'><pre id='7vGAe'><center id='7vGAe'></center></pre></bdo></b><th id='7vGAe'></th></span></q></dt></tr></i><div id='7vGAe'><tfoot id='7vGAe'></tfoot><dl id='7vGAe'><fieldset id='7vGAe'></fieldset></dl></div>

                  本文介绍了使用 pandas 从 csv 文件中读回元组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  使用 pandas,我已将一个数据框导出到一个 csv 文件,该数据框的单元格包含字符串元组.生成的文件具有以下结构:

                  Using pandas, I have exported to a csv file a dataframe whose cells contain tuples of strings. The resulting file has the following structure:

                  index,colA
                  1,"('a','b')"
                  2,"('c','d')"
                  

                  现在我想使用 read_csv 读回它.但是无论我尝试什么,pandas 都会将这些值解释为字符串而不是元组.例如:

                  Now I want to read it back using read_csv. However whatever I try, pandas interprets the values as strings rather than tuples. For instance:

                  In []: import pandas as pd
                         df = pd.read_csv('test',index_col='index',dtype={'colA':tuple})
                         df.loc[1,'colA']
                  Out[]: "('a','b')"
                  

                  有没有办法告诉熊猫做正确的事?最好不要对数据框进行繁重的后处理:实际表有 5000 行和 2500 列.

                  Is there a way of telling pandas to do the right thing? Preferably without heavy post-processing of the dataframe: the actual table has 5000 rows and 2500 columns.

                  推荐答案

                  在列中存储元组通常不是一个好主意;使用 Series 和 DataFrame 的许多优点都丢失了.也就是说,您可以使用 converters 对字符串进行后处理:

                  Storing tuples in a column isn't usually a good idea; a lot of the advantages of using Series and DataFrames are lost. That said, you could use converters to post-process the string:

                  >>> df = pd.read_csv("sillytup.csv", converters={"colA": ast.literal_eval})
                  >>> df
                     index    colA
                  0      1  (a, b)
                  1      2  (c, d)
                  
                  [2 rows x 2 columns]
                  >>> df.colA.iloc[0]
                  ('a', 'b')
                  >>> type(df.colA.iloc[0])
                  <type 'tuple'>
                  

                  但我可能会在源代码上进行更改以避免首先存储元组.

                  But I'd probably change things at source to avoid storing tuples in the first place.

                  这篇关于使用 pandas 从 csv 文件中读回元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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='h4ObH'><tr id='h4ObH'><dt id='h4ObH'><q id='h4ObH'><span id='h4ObH'><b id='h4ObH'><form id='h4ObH'><ins id='h4ObH'></ins><ul id='h4ObH'></ul><sub id='h4ObH'></sub></form><legend id='h4ObH'></legend><bdo id='h4ObH'><pre id='h4ObH'><center id='h4ObH'></center></pre></bdo></b><th id='h4ObH'></th></span></q></dt></tr></i><div id='h4ObH'><tfoot id='h4ObH'></tfoot><dl id='h4ObH'><fieldset id='h4ObH'></fieldset></dl></div>
                  • <tfoot id='h4ObH'></tfoot>
                    <legend id='h4ObH'><style id='h4ObH'><dir id='h4ObH'><q id='h4ObH'></q></dir></style></legend>

                          <tbody id='h4ObH'></tbody>
                          <bdo id='h4ObH'></bdo><ul id='h4ObH'></ul>

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