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

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

      <tfoot id='0Nqxj'></tfoot>

      1. 使用agg&amp;联接对一列进行分组,但仅按唯一值进行分组(&A)

        Grouping one column using agg amp; join but only on unique values(使用aggamp;联接对一列进行分组,但仅按唯一值进行分组(A))
        • <tfoot id='eUCf7'></tfoot>

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

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

                  <legend id='eUCf7'><style id='eUCf7'><dir id='eUCf7'><q id='eUCf7'></q></dir></style></legend>
                  本文介绍了使用agg&amp;联接对一列进行分组,但仅按唯一值进行分组(&A)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在以下数据集上使用了这段巧妙的代码

                      df = pd.DataFrame({
                      'contact_email': ['info@info.com', 'info@info.com', 'info@info.com'], 
                      'interest': ['Math', 'Science', 'Science']
                  })
                      print(df)
                      interest contact_email
                  0   Math    info@info.com
                  1   Science info@info.com
                  2   Science info@info.com
                  
                  df = df.groupby('Contact_Email').agg({'interest' : ' '.join}).reset_index()
                  print(df)
                  
                          contact_email   AOI
                  0   info@info.com   Math Science Science
                  

                  这与我想要的非常接近,但我只需要返回唯一的利息。(我有用户/客户输入相同的表单,几乎10次输入相同的值!)

                  还有,有没有人知道如何删除0,1,2,3索引,这是一件好事。

                  谢谢!

                  推荐答案

                  使用unique删除重复项:

                  df = (df.groupby('contact_email')
                          .agg({'interest' : lambda x: ' '.join(x.unique())})
                          .reset_index())
                  print(df)
                     contact_email      interest
                  0  info@info.com  Math Science
                  

                  sets,但应更改值的顺序:

                  df = df.groupby('contact_email').agg({'interest' : lambda x: ' '.join(set(x))}).reset_index()
                  print(df)
                     contact_email      interest
                  0  info@info.com  Math Science
                  

                  drop_duplicates

                  df = (df.drop_duplicates(subset=['contact_email','interest'])
                         .groupby('contact_email')
                         .agg({'interest' : ' '.join})
                         .reset_index())
                  print(df)
                     contact_email      interest
                  0  info@info.com  Math Science
                  

                  这篇关于使用agg&amp;联接对一列进行分组,但仅按唯一值进行分组(&A)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)
                  Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)
                  Is there a way of group by month in Pandas starting at specific day number?( pandas 有从特定日期开始的按月分组的方式吗?)
                  Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)
                  Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)
                  Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)
                      <tbody id='jaroE'></tbody>

                  • <tfoot id='jaroE'></tfoot>

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

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

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