• <bdo id='kGQYI'></bdo><ul id='kGQYI'></ul>

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

        <tfoot id='kGQYI'></tfoot><legend id='kGQYI'><style id='kGQYI'><dir id='kGQYI'><q id='kGQYI'></q></dir></style></legend>

        <i id='kGQYI'><tr id='kGQYI'><dt id='kGQYI'><q id='kGQYI'><span id='kGQYI'><b id='kGQYI'><form id='kGQYI'><ins id='kGQYI'></ins><ul id='kGQYI'></ul><sub id='kGQYI'></sub></form><legend id='kGQYI'></legend><bdo id='kGQYI'><pre id='kGQYI'><center id='kGQYI'></center></pre></bdo></b><th id='kGQYI'></th></span></q></dt></tr></i><div id='kGQYI'><tfoot id='kGQYI'></tfoot><dl id='kGQYI'><fieldset id='kGQYI'></fieldset></dl></div>
      1. 如何将项目附加到 Pandas 中不同列的列表中

        时间:2023-09-27

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

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

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

                  <legend id='bzpVW'><style id='bzpVW'><dir id='bzpVW'><q id='bzpVW'></q></dir></style></legend>
                    <tbody id='bzpVW'></tbody>
                  本文介绍了如何将项目附加到 Pandas 中不同列的列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个如下所示的数据框:

                  I have a dataframe that looks like this:

                  dic = {'A':['PINCO','PALLO','CAPPO','ALLOP'],
                         'B':['KILO','KULO','FIGA','GAGO'],
                         'C':[['CAL','GOL','TOA','PIA','STO'],
                              ['LOL','DAL','ERS','BUS','TIS'],
                              ['PIS','IPS','ZSP','YAS','TUS'],
                              []]}
                  df1 = pd.DataFrame(dic)
                  

                  我的目标是为每一行插入 A 的元素作为列 C 中包含的列表的第一项.同时我想将 B 的元素设置为 C 中包含的列表的最后一项.

                  My goal is to insert for each row the element of A as first item of the list contained in column C. At the same time I want to set the element of B as last item of the list contained in C.

                  我能够通过使用以下代码行来实现我的目标:

                  I was able to achieve my goal by using the following lines of code:

                  for index, row in df1.iterrows():
                      try:
                          row['C'].insert(0,row['A'])
                          row['C'].append(row['B'])
                      except:
                          pass
                  

                  是否有更优雅、更有效的方法来实现我的目标,也许是使用一些 Pandas 功能?我想尽可能避免 for 循环.

                  Is there a more elegant and efficient way to achieve my goal maybe using some Pandas function? I would like to avoid for loops possibly.

                  推荐答案

                  一个好的一般规则是尽可能避免使用 applyaxis=1 作为迭代在行上是昂贵的

                  A good general rule is to avoid using apply with axis=1 if at all possible as iterating over the rows is expenisve

                  您可以使用 map 将 A 列和 B 列中的每个元素转换为列表,然后对各行求和.

                  You can convert each element in columns A and B to a list with map and then sum across the rows.

                  df1['A'] = df1.A.map(lambda x: [x])  
                  df1['B'] = df1.B.map(lambda x: [x])
                  df1.sum(1)
                  CPU times: user 3.07 s, sys: 207 ms, total: 3.27 s
                  

                  替代方法是使用轴=1 的 apply 在我的计算机上运行 100 万行时慢 15 倍

                  The alternative is to use apply with axis=1 which ran 15 times slower on my computer on 1 million rows

                  df1.apply(lambda x: [x['A']] + x['C'] + [x['B']], 1)
                  CPU times: user 48.5 s, sys: 119 ms, total: 48.6 s
                  

                  这篇关于如何将项目附加到 Pandas 中不同列的列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:将项目附加到列表列表中的指定列表(Python) 下一篇:将函数输出附加到多个列表的 Pythonic 方法

                  相关文章

                2. <small id='GYnm9'></small><noframes id='GYnm9'>

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

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

                        <bdo id='GYnm9'></bdo><ul id='GYnm9'></ul>
                    1. <tfoot id='GYnm9'></tfoot>