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

    <tfoot id='GXCGK'></tfoot>

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

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

      集合的交集作为 pandas 中的列

      Intersection of sets as columns in pandas(集合的交集作为 pandas 中的列)
        <i id='wDLyU'><tr id='wDLyU'><dt id='wDLyU'><q id='wDLyU'><span id='wDLyU'><b id='wDLyU'><form id='wDLyU'><ins id='wDLyU'></ins><ul id='wDLyU'></ul><sub id='wDLyU'></sub></form><legend id='wDLyU'></legend><bdo id='wDLyU'><pre id='wDLyU'><center id='wDLyU'></center></pre></bdo></b><th id='wDLyU'></th></span></q></dt></tr></i><div id='wDLyU'><tfoot id='wDLyU'></tfoot><dl id='wDLyU'><fieldset id='wDLyU'></fieldset></dl></div>

          <tbody id='wDLyU'></tbody>

          <tfoot id='wDLyU'></tfoot>

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

            <legend id='wDLyU'><style id='wDLyU'><dir id='wDLyU'><q id='wDLyU'></q></dir></style></legend>
              <bdo id='wDLyU'></bdo><ul id='wDLyU'></ul>
              1. 本文介绍了集合的交集作为 pandas 中的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个 df,例如:

                I have a df such as:

                df=pd.DataFrame.from_items([('i', [set([1,2,3,4]), set([1,2,3,4]), set([1,2,3,4]),set([1,2,3,4])]), ('j', [set([2,3]), set([1]), set([4]),set([3,4])])])
                

                看起来像

                >>> df
                              i       j
                0  {1, 2, 3, 4}  {2, 3}
                1  {1, 2, 3, 4}     {1}
                2  {1, 2, 3, 4}     {4}
                3  {1, 2, 3, 4}  {3, 4}
                

                我想计算 df.i.intersection(df.j) 并将其指定为 k 列.也就是说,我想要这个:

                I would like to compute df.i.intersection(df.j) and assign that to be column k. That is, I want this:

                df['k']=[df.i.iloc[t].intersection(df.j.iloc[t]) for t in range(4)]
                
                >>> df.k
                0    {2, 3}
                1       {1}
                2       {4}
                3    {3, 4}
                Name: k, dtype: object
                

                这个有 df.apply() 吗?实际的 df 是数百万行.

                Is there a df.apply() for this? The actual df is millions of rows.

                推荐答案

                使用 sets, lists 和 dicts in pandas 有点问题,因为最好使用标量:

                Working with sets, lists and dicts in pandas is a bit problematic, because best working with scalars:

                df['k'] = [x[0] & x[1] for x in zip(df['i'], df['j'])]
                print (df)
                              i       j       k
                0  {1, 2, 3, 4}  {2, 3}  {2, 3}
                1  {1, 2, 3, 4}     {1}     {1}
                2  {1, 2, 3, 4}     {4}     {4}
                3  {1, 2, 3, 4}  {3, 4}  {3, 4}
                

                <小时>

                df['k'] = [x[0].intersection(x[1]) for x in zip(df['i'], df['j'])]
                print (df)
                              i       j       k
                0  {1, 2, 3, 4}  {2, 3}  {2, 3}
                1  {1, 2, 3, 4}     {1}     {1}
                2  {1, 2, 3, 4}     {4}     {4}
                3  {1, 2, 3, 4}  {3, 4}  {3, 4}
                

                应用的解决方案:

                df['k'] = df.apply(lambda x: x['i'].intersection(x['j']), axis=1)
                print (df)
                              i       j       k
                0  {1, 2, 3, 4}  {2, 3}  {2, 3}
                1  {1, 2, 3, 4}     {1}     {1}
                2  {1, 2, 3, 4}     {4}     {4}
                3  {1, 2, 3, 4}  {3, 4}  {3, 4}
                

                这篇关于集合的交集作为 pandas 中的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                Pythonic and efficient way of finding adjacent cells in grid(在网格中查找相邻单元格的 Pythonic 和有效方法)
                map a hexagonal grid in matplotlib(在 matplotlib 中映射六边形网格)
                Execute arbitrary python code remotely - can it be done?(远程执行任意 python 代码 - 可以吗?)
                Python - Plotting colored grid based on values(Python - 根据值绘制彩色网格)
                Is there a GUI design app for the Tkinter / grid geometry?(是否有 Tkinter/网格几何图形的 GUI 设计应用程序?)
                tkinter Canvas Scrollbar with Grid?(带有网格的 tkinter 画布滚动条?)
                  <legend id='XGHYp'><style id='XGHYp'><dir id='XGHYp'><q id='XGHYp'></q></dir></style></legend>
                    <bdo id='XGHYp'></bdo><ul id='XGHYp'></ul>

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

                      <tbody id='XGHYp'></tbody>

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