python-3.x - 怀疑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 文件。感谢关注
解决方案
需要这个吗?
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
推荐阅读
- php - file_get_contents 拒绝连接 centos7
- python - 如何对数据数组进行数值拉普拉斯逆变换?
- sql - Db2 for I:删除时如何选择行?
- python - 如何在字典字符串周围使用 f 字符串格式
- angular - 分叉的 npm / Angular 8 包安装方式不同原始版本
- python - 如何在 Python 中使用多处理定期调用函数?
- java - TableLayout 之外的边距
- java - NoClassDefFoundError 通过反射从依赖类获取方法
- python - 使用 Pandas 单独导出表列表
- python - 使用 Flask 流式传输视频文件