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

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

        <i id='asyUX'><tr id='asyUX'><dt id='asyUX'><q id='asyUX'><span id='asyUX'><b id='asyUX'><form id='asyUX'><ins id='asyUX'></ins><ul id='asyUX'></ul><sub id='asyUX'></sub></form><legend id='asyUX'></legend><bdo id='asyUX'><pre id='asyUX'><center id='asyUX'></center></pre></bdo></b><th id='asyUX'></th></span></q></dt></tr></i><div id='asyUX'><tfoot id='asyUX'></tfoot><dl id='asyUX'><fieldset id='asyUX'></fieldset></dl></div>
      1. <tfoot id='asyUX'></tfoot>
          <bdo id='asyUX'></bdo><ul id='asyUX'></ul>
      2. 将 Pandas 的列表列拆分为多列

        Split a Pandas column of lists into multiple columns(将 Pandas 的列表列拆分为多列)
      3. <i id='wXWGm'><tr id='wXWGm'><dt id='wXWGm'><q id='wXWGm'><span id='wXWGm'><b id='wXWGm'><form id='wXWGm'><ins id='wXWGm'></ins><ul id='wXWGm'></ul><sub id='wXWGm'></sub></form><legend id='wXWGm'></legend><bdo id='wXWGm'><pre id='wXWGm'><center id='wXWGm'></center></pre></bdo></b><th id='wXWGm'></th></span></q></dt></tr></i><div id='wXWGm'><tfoot id='wXWGm'></tfoot><dl id='wXWGm'><fieldset id='wXWGm'></fieldset></dl></div>
        • <bdo id='wXWGm'></bdo><ul id='wXWGm'></ul>

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

            <tfoot id='wXWGm'></tfoot>

            <legend id='wXWGm'><style id='wXWGm'><dir id='wXWGm'><q id='wXWGm'></q></dir></style></legend>
                    <tbody id='wXWGm'></tbody>
                  本文介绍了将 Pandas 的列表列拆分为多列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个带有一列的 Pandas DataFrame:

                  I have a Pandas DataFrame with one column:

                  df = pd.DataFrame({"teams": [["SF", "NYG"] for _ in range(7)]})
                  
                         teams
                  0  [SF, NYG]
                  1  [SF, NYG]
                  2  [SF, NYG]
                  3  [SF, NYG]
                  4  [SF, NYG]
                  5  [SF, NYG]
                  6  [SF, NYG]
                  

                  如何将这一列列表分成两列?

                  How can split this column of lists into two columns?

                  想要的结果:

                    team1 team2
                  0    SF   NYG
                  1    SF   NYG
                  2    SF   NYG
                  3    SF   NYG
                  4    SF   NYG
                  5    SF   NYG
                  6    SF   NYG
                  

                  推荐答案

                  您可以将 DataFrame 构造函数与 lists 一起使用pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.to_list.html" rel="noreferrer">to_list:

                  You can use the DataFrame constructor with lists created by to_list:

                  import pandas as pd
                  
                  d1 = {'teams': [['SF', 'NYG'],['SF', 'NYG'],['SF', 'NYG'],
                                  ['SF', 'NYG'],['SF', 'NYG'],['SF', 'NYG'],['SF', 'NYG']]}
                  df2 = pd.DataFrame(d1)
                  print (df2)
                         teams
                  0  [SF, NYG]
                  1  [SF, NYG]
                  2  [SF, NYG]
                  3  [SF, NYG]
                  4  [SF, NYG]
                  5  [SF, NYG]
                  6  [SF, NYG]
                  


                  df2[['team1','team2']] = pd.DataFrame(df2.teams.tolist(), index= df2.index)
                  print (df2)
                         teams team1 team2
                  0  [SF, NYG]    SF   NYG
                  1  [SF, NYG]    SF   NYG
                  2  [SF, NYG]    SF   NYG
                  3  [SF, NYG]    SF   NYG
                  4  [SF, NYG]    SF   NYG
                  5  [SF, NYG]    SF   NYG
                  6  [SF, NYG]    SF   NYG
                  

                  对于一个新的DataFrame:

                  df3 = pd.DataFrame(df2['teams'].to_list(), columns=['team1','team2'])
                  print (df3)
                    team1 team2
                  0    SF   NYG
                  1    SF   NYG
                  2    SF   NYG
                  3    SF   NYG
                  4    SF   NYG
                  5    SF   NYG
                  6    SF   NYG
                  

                  使用 apply(pd.Series) 的解决方案非常慢:

                  A solution with apply(pd.Series) is very slow:

                  #7k rows
                  df2 = pd.concat([df2]*1000).reset_index(drop=True)
                  
                  In [121]: %timeit df2['teams'].apply(pd.Series)
                  1.79 s ± 52.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
                  
                  In [122]: %timeit pd.DataFrame(df2['teams'].to_list(), columns=['team1','team2'])
                  1.63 ms ± 54.3 s per loop (mean ± std. dev. of 7 runs, 1000 loops each)
                  

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

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

                  相关文档推荐

                  How does the @property decorator work in Python?(@property 装饰器在 Python 中是如何工作的?)
                  What is the difference between old style and new style classes in Python?(Python中的旧样式类和新样式类有什么区别?)
                  How to break out of multiple loops?(如何打破多个循环?)
                  How to put the legend out of the plot(如何将传说从情节中剔除)
                  Why is the output of my function printing out quot;Nonequot;?(为什么我的函数输出打印出“无?)
                  Difference between modes a, a+, w, w+, and r+ in built-in open function?(内置 open 函数中模式 a、a+、w、w+ 和 r+ 的区别?)
                  • <bdo id='PQD5r'></bdo><ul id='PQD5r'></ul>

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

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

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

                          1. <legend id='PQD5r'><style id='PQD5r'><dir id='PQD5r'><q id='PQD5r'></q></dir></style></legend>