首页 > 解决方案 > 怀疑pandas逐行过滤数据

问题描述

我该如何解决这个与熊猫相关的问题?我有以下方法的数据框:

日期时间64ns 类型(整数) datetime64ns(分析)
2019-02-02T10:02:05 4
2019-02-02T10:02:01 3
2019-02-02T10:02:02 4 2019-02-02T10:02:02
2019-02-02T10:02:04 3 2019-02-02T10:02:04

目标是执行以下问题:

# psuedocode
for all the rows:
  if datetime(analysis) exists and type=4:
      insert in the a new row column type4=1
  
  elseif datetime(analysis) exists and type=2:
      insert in the a new row column type2=1

开发它的想法是为了按计数值创建一个组。我确信这是可能的,因为我过去设法开发它,但我丢失了我的 .py 文件。感谢关注

标签: python-3.xpandasnumpy

解决方案


需要这个吗?

df = pd.concat([df, pd.get_dummies(df['type(int)'].mask(
    df['datetime64ns(analysis)'].isna()).astype('Int64')).add_prefix('type')], 1)

输出:

          datetime64ns  type(int) datetime64ns(analysis)  type3  type4
0  2019-02-02T10:02:05          4                    NaN      0      0
1  2019-02-02T10:02:01          3                    NaN      0      0
2  2019-02-02T10:02:02          4    2019-02-02T10:02:02      0      1
3  2019-02-02T10:02:04          3    2019-02-02T10:02:04      1      0



推荐阅读