如何在pandas

时间:2018-05-30 10:13:43

标签: python-3.x pandas

我在数据框中存储了以下数据,我想删除id等于finalid的行,对于相同的id,我有多行。

例如: df_target

id finalid month year count_ph count_sh
1   1      1     2012  12        20
1   2      1     2012   6         18
1   32     1     2012   6         2
2   2      1     2012   2         6
2   23     1     2012   2         6
3   3      1     2012   2         2

输出

id finalid month year count_ph count_sh

    1   2    1     2012    6         18
    1   32   1     2012    6         2
    2   23   1     2012    2         6
    3   3    1     2012    2         2

功能类似于: 删除记录并获取最终的数据帧 (df_target.groupby([' id',' month'' year'])。size()> 1)& (df_target [' id'] == df_target [' finalid'])

1 个答案:

答案 0 :(得分:1)

我认为{for} Series需要DataFrame作为原始~df = df_target[~((df_target.groupby(['id','month','year'])['id'].transform('size') > 1) & (df_target['id'] == df_target['finalid']))] 需要反转最终布尔值掩码:

df = df_target[((df_target.groupby(['id','month','year'])['id'].transform('size') <= 1) | 
                 (df_target['id'] != df_target['finalid']))]

替代解决方案:

print (df)
   id  finalid  month  year  count_ph  count_sh
1   1        2      1  2012         6        18
2   1       32      1  2012         6         2
4   2       23      1  2012         2         6
5   3        3      1  2012         2         2
import pako from 'pako';
import isFunction from 'lodash/isFunction';
import untar from 'js-untar';

class FileStreamer {
  ...
}

export async function decompress(zipfile, onProgress) {
  const fs = new FileStreamer(zipfile);
  const inflator = new pako.Inflate();
  let block;

  while (!fs.isEndOfFile()) {
    block = await fs.readBlock();
    inflator.push(block.data, fs.isEndOfFile());
    if (inflator.err) {
      throw inflator.err
    }
    if (isFunction(onProgress)) onProgress(block.progress)
  }
  return await untar(inflator.result.buffer);
}