<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 中的列

      时间:2023-07-24
        <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 中的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:删除二维数组中无序重复项最省时的方法是什么? 下一篇:使用 ldap python 更新 Active Directory 密码

                相关文章

                1. <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'>

                    <tfoot id='XGHYp'></tfoot>
                  1. <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>