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

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

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

      PANAS-基于列对行进行分组,并将NaN替换为非空值

      Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)

      • <legend id='g9JpQ'><style id='g9JpQ'><dir id='g9JpQ'><q id='g9JpQ'></q></dir></style></legend>

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

        <tfoot id='g9JpQ'></tfoot>
              <bdo id='g9JpQ'></bdo><ul id='g9JpQ'></ul>
                <tbody id='g9JpQ'></tbody>

                <i id='g9JpQ'><tr id='g9JpQ'><dt id='g9JpQ'><q id='g9JpQ'><span id='g9JpQ'><b id='g9JpQ'><form id='g9JpQ'><ins id='g9JpQ'></ins><ul id='g9JpQ'></ul><sub id='g9JpQ'></sub></form><legend id='g9JpQ'></legend><bdo id='g9JpQ'><pre id='g9JpQ'><center id='g9JpQ'></center></pre></bdo></b><th id='g9JpQ'></th></span></q></dt></tr></i><div id='g9JpQ'><tfoot id='g9JpQ'></tfoot><dl id='g9JpQ'><fieldset id='g9JpQ'></fieldset></dl></div>
                本文介绍了PANAS-基于列对行进行分组,并将NaN替换为非空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试基于目标GROUP-BY";列在我的数据框上使用字符串创建一些聚合。

                假设我有以下4列数据帧:

                我要基于列";col1";对所有行进行分组,在此情况下,使用非NULL值进行分组。

                所需输出如下:

                我还尝试使用正常:

                import pandas as pd
                from tabulate import tabulate
                
                df = pd.DataFrame({'Col1': ['A', 'B', 'A'],
                                   'Col2': ['X', 'Z', 'X'],
                                   'Col3': ['Y', 'D', ''],
                                   'Col4': ['', 'E', 'V'],})
                
                print(tabulate(df, headers='keys', tablefmt='psql'))
                df2 = df.groupby(['Col1'])
                print(tabulate(df2, headers='keys', tablefmt='psql'))
                

                但它不对NaN值进行分组.

                如何执行此操作?

                谢谢!

                推荐答案

                如果可能,只需使用GroupBy.first针对每个组的第一个非缺失值提出问题:

                df = pd.DataFrame({'Col1': ['A', 'B', 'A'],
                                   'Col2': ['X', 'Z', 'X'],
                                   'Col3': ['Y', 'D', np.nan],
                                   'Col4': [np.nan, 'E', 'V'],})
                
                
                df2 = df.groupby(['Col1'], as_index=False).first()
                print (df2)
                  Col1 Col2 Col3 Col4
                0    A    X    Y    V
                1    B    Z    D    E
                

                这篇关于PANAS-基于列对行进行分组,并将NaN替换为非空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                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算法和插值算法)
                Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)

                  <small id='1XR6F'></small><noframes id='1XR6F'>

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