首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python -在满足特定条件时在文本文件中添加列

Python -在满足特定条件时在文本文件中添加列
EN

Stack Overflow用户
提问于 2022-10-11 10:23:41
回答 1查看 24关注 0票数 0

我想要创建一个python脚本,它将执行以下步骤,但是我不太确定如何处理它。我有一个类似于此的文本文件(这是一个被转换为.tsv.txt文件):

代码语言:javascript
复制
trans_x trans_y trans_z rot_x   rot_y   rot_z   non_steady_state_outlier00  non_steady_state_outlier01  motion_outlier00    motion_outlier01    motion_outlier02    motion_outlier03    motion_outlier04    motion_outlier05    motion_outlier06    motion_outlier07    motion_outlier08    motion_outlier09    motion_outlier10    motion_outlier11    motion_outlier12    motion_outlier13    motion_outlier14    motion_outlier15    motion_outlier16    motion_outlier17    motion_outlier18    motion_outlier19    motion_outlier20    motion_outlier21    motion_outlier22    motion_outlier23    motion_outlier24    motion_outlier25    motion_outlier26    motion_outlier27    motion_outlier28    motion_outlier29    motion_outlier30    motion_outlier31    motion_outlier32    motion_outlier33    motion_outlier34    motion_outlier35    motion_outlier36    motion_outlier37    motion_outlier38    motion_outlier39    motion_outlier40    motion_outlier41    motion_outlier42    motion_outlier43    motion_outlier44    motion_outlier45    motion_outlier46    motion_outlier47    motion_outlier48    motion_outlier49    motion_outlier50    motion_outlier51    motion_outlier52    motion_outlier53    motion_outlier54    motion_outlier55    motion_outlier56    motion_outlier57
-0.213045   0.188054    -0.0350898  -0.000904171    -0.001059   0.00130768  1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.310081    -0.194011   0.00361878  0.000139216 0.000693861 -0.00162252 0.0 1.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.120743    -0.328317   0.00325342  -0.000296934    0.000299405 -0.000840907    0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.17164 -0.36066    -0.00439546 -0.000190615    0.000769665 -0.000238167    0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0

我需要的是一个密码:

  1. 遍历我的.txt文件和搜索,查看是否有一个名为non_steady_state_outlier02的列不存在,在non_steady_state_outlier01之后添加一个名为non_steady_state_outlier01的列,并填写它,以便在第三个位置上有一个1,在其他任何地方都有一个0(例如,01.0 0 0.)。

事先非常感谢!

EN

回答 1

Stack Overflow用户

发布于 2022-10-11 11:17:35

假设你的桌子是这样的:

等。

然后循环遍历python中的文本文件目录,并根据您的情况使用熊猫可以很好地工作:

代码语言:javascript
复制
import pandas as pd
import os
for filename in os.listdir('.'):
    if filename.endswith('.txt'):
        df = pd.read_csv(filename)
        if not 'non_steady_state_outlier02' in df.columns:
            df['non_steady_state_outlier02'] = pd.Series([0, 0, 1, 0])
            df.to_csv(filename.replace('.txt', '_new.txt'))
        else:
            pass

如果不是.csv,则将分隔符添加到熊猫中,如选项卡:

代码语言:javascript
复制
df = pd.read_csv(filename, delimiter='\t)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74026567

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档