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

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

      <bdo id='fa7vb'></bdo><ul id='fa7vb'></ul>
  3. <tfoot id='fa7vb'></tfoot>

    1. pandas 将值与前一行与过滤条件进行比较

      Pandas compare value with previous row with filtration condition( pandas 将值与前一行与过滤条件进行比较)

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

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

                  <tbody id='W1JpJ'></tbody>
                <tfoot id='W1JpJ'></tfoot>

              1. 本文介绍了 pandas 将值与前一行与过滤条件进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个包含员工工资信息的 DataFrame.大约有 900000+ 行.

                I have a DataFrame with information about employee salary. It's about 900000+ rows.

                示例:

                +----+-------------+---------------+----------+
                |    |   table_num | name          |   salary |
                |----+-------------+---------------+----------|
                |  0 |      001234 | John Johnson  |     1200 |
                |  1 |      001234 | John Johnson  |     1000 |
                |  2 |      001235 | John Johnson  |     1000 |
                |  3 |      001235 | John Johnson  |     1200 |
                |  4 |      001235 | John Johnson  |     1000 |
                |  5 |      001235 | Steve Stevens |     1000 |
                |  6 |      001236 | Steve Stevens |     1200 |
                |  7 |      001236 | Steve Stevens |     1200 |
                |  8 |      001236 | Steve Stevens |     1200 |
                +----+-------------+---------------+----------+
                

                数据类型:

                table_num: string
                name: string
                salary: float
                

                我需要添加一列,其中包含有关增加减少的工资水平的信息.我正在使用 shift() 函数来比较行中的值.

                I need to add a column with information about increaseddecreased salary level. I'm using the shift() function to compare value in rows.

                主要问题在于对整个数据集的所有唯一员工进行过滤和迭代.

                Main problem is in filtration and iteration over all unique employees over the whole dataset.

                在我的脚本中大约需要 3 个半小时.

                如何做到更快?

                我的脚本:

                # giving us only unique combination of 'table_num' and 'name'
                    # since there can be same 'table_num' for different 'name'
                    # and same names with different 'table_num' appears sometimes
                
                names_df = df[['table_num', 'name']].drop_duplicates()
                
                # then extracting particular name and table_num from Series
                for i in range(len(names_df)):    ### Bottleneck of whole script ###    
                    t = names_df.iloc[i,[0,1]][0]
                    n = names_df.iloc[i,[0,1]][1]
                
                    # using shift() and lambda to check if there difference between two rows 
                    diff_sal = (df[(df['table_num']==t)
                               & ((df['name']==n))]['salary'] - df[(df['table_num']==t)
                                                                 & ((df['name']==n))]['salary'].shift(1)).apply(lambda x: 1 if x>0 else (-1 if x<0 else 0))
                    df.loc[diff_sal.index, 'inc'] = diff_sal.values
                

                示例输入数据:

                df = pd.DataFrame({'table_num': ['001234','001234','001235','001235','001235','001235','001236','001236','001236'], 
                                     'name': ['John Johnson','John Johnson','John Johnson','John Johnson','John Johnson', 'Steve Stevens', 'Steve Stevens', 'Steve Stevens', 'Steve Stevens'], 
                                     'salary':[1200.,1000.,1000.,1200.,1000.,1000.,1200.,1200.,1200.]})
                

                样本输出:

                +----+-------------+---------------+----------+-------+
                |    |   table_num | name          |   salary |   inc |
                |----+-------------+---------------+----------+-------|
                |  0 |      001234 | John Johnson  |     1200 |     0 |
                |  1 |      001234 | John Johnson  |     1000 |    -1 |
                |  2 |      001235 | John Johnson  |     1000 |     0 |
                |  3 |      001235 | John Johnson  |     1200 |     1 |
                |  4 |      001235 | John Johnson  |     1000 |    -1 |
                |  5 |      001235 | Steve Stevens |     1000 |     0 |
                |  6 |      001236 | Steve Stevens |     1200 |     0 |
                |  7 |      001236 | Steve Stevens |     1200 |     0 |
                |  8 |      001236 | Steve Stevens |     1200 |     0 |
                +----+-------------+---------------+----------+-------+
                

                推荐答案

                使用 groupbydiff:

                df['inc'] = df.groupby(['table_num', 'name'])['salary'].diff().fillna(0.0)
                df.loc[df['inc'] > 0.0, 'inc'] = 1.0
                df.loc[df['inc'] < 0.0, 'inc'] = -1.0
                

                这篇关于 pandas 将值与前一行与过滤条件进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                What happens when you compare 2 pandas Series(当你比较 2 个 pandas 系列时会发生什么)
                Quickly find differences between two large text files(快速查找两个大文本文件之间的差异)
                Python - Compare 2 files and output differences(Python - 比较 2 个文件和输出差异)
                Why do comparisions between very large float values fail in python?(为什么在 python 中非常大的浮点值之间的比较会失败?)
                Dictionary merge by updating but not overwriting if value exists(字典通过更新合并,但如果值存在则不覆盖)
                Find entries of one text file in another file in python(在python中的另一个文件中查找一个文本文件的条目)

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

                        <tbody id='45Psl'></tbody>

                      <small id='45Psl'></small><noframes id='45Psl'>

                      <tfoot id='45Psl'></tfoot>

                    1. <legend id='45Psl'><style id='45Psl'><dir id='45Psl'><q id='45Psl'></q></dir></style></legend>