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

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

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

      1. <tfoot id='jnbNn'></tfoot>

        拆分(分解) pandas 数据框字符串条目以分隔行

        时间:2024-04-21

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

          <tbody id='xzlGo'></tbody>
          <legend id='xzlGo'><style id='xzlGo'><dir id='xzlGo'><q id='xzlGo'></q></dir></style></legend>

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

            • <tfoot id='xzlGo'></tfoot>
                  <bdo id='xzlGo'></bdo><ul id='xzlGo'></ul>

                • 本文介绍了拆分(分解) pandas 数据框字符串条目以分隔行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  I have a pandas dataframe in which one column of text strings contains comma-separated values. I want to split each CSV field and create a new row per entry (assume that CSV are clean and need only be split on ','). For example, a should become b:

                  In [7]: a
                  Out[7]: 
                      var1  var2
                  0  a,b,c     1
                  1  d,e,f     2
                  
                  In [8]: b
                  Out[8]: 
                    var1  var2
                  0    a     1
                  1    b     1
                  2    c     1
                  3    d     2
                  4    e     2
                  5    f     2
                  

                  So far, I have tried various simple functions, but the .apply method seems to only accept one row as return value when it is used on an axis, and I can't get .transform to work. Any suggestions would be much appreciated!

                  Example data:

                  from pandas import DataFrame
                  import numpy as np
                  a = DataFrame([{'var1': 'a,b,c', 'var2': 1},
                                 {'var1': 'd,e,f', 'var2': 2}])
                  b = DataFrame([{'var1': 'a', 'var2': 1},
                                 {'var1': 'b', 'var2': 1},
                                 {'var1': 'c', 'var2': 1},
                                 {'var1': 'd', 'var2': 2},
                                 {'var1': 'e', 'var2': 2},
                                 {'var1': 'f', 'var2': 2}])
                  

                  I know this won't work because we lose DataFrame meta-data by going through numpy, but it should give you a sense of what I tried to do:

                  def fun(row):
                      letters = row['var1']
                      letters = letters.split(',')
                      out = np.array([row] * len(letters))
                      out['var1'] = letters
                  a['idx'] = range(a.shape[0])
                  z = a.groupby('idx')
                  z.transform(fun)
                  

                  解决方案

                  How about something like this:

                  In [55]: pd.concat([Series(row['var2'], row['var1'].split(','))              
                                      for _, row in a.iterrows()]).reset_index()
                  Out[55]: 
                    index  0
                  0     a  1
                  1     b  1
                  2     c  1
                  3     d  2
                  4     e  2
                  5     f  2
                  

                  Then you just have to rename the columns

                  这篇关于拆分(分解) pandas 数据框字符串条目以分隔行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 Python 的 SQL 语句中使用变量? 下一篇:在 Python 中展平一个浅列表

                  相关文章

                  • <bdo id='4WgzA'></bdo><ul id='4WgzA'></ul>
                  1. <tfoot id='4WgzA'></tfoot>
                  2. <small id='4WgzA'></small><noframes id='4WgzA'>

                      <i id='4WgzA'><tr id='4WgzA'><dt id='4WgzA'><q id='4WgzA'><span id='4WgzA'><b id='4WgzA'><form id='4WgzA'><ins id='4WgzA'></ins><ul id='4WgzA'></ul><sub id='4WgzA'></sub></form><legend id='4WgzA'></legend><bdo id='4WgzA'><pre id='4WgzA'><center id='4WgzA'></center></pre></bdo></b><th id='4WgzA'></th></span></q></dt></tr></i><div id='4WgzA'><tfoot id='4WgzA'></tfoot><dl id='4WgzA'><fieldset id='4WgzA'></fieldset></dl></div>
                      <legend id='4WgzA'><style id='4WgzA'><dir id='4WgzA'><q id='4WgzA'></q></dir></style></legend>