用 for 循环内数据框列中的空格替换 \t

时间:2021-01-18 08:51:03

标签: python pandas

我目前遇到的问题是用空格替换 \t 并在每个 df{i}

中分成 2 列
import os
import pandas as pd
glb = globals()

keywords1 = ['input5']
i=1
for path, dirs, files in os.walk('.'):
    for file in files:
        if any(key in file for key in keywords1) and file.endswith('.txt'):
            filename1 = os.path.join(path, file)
            print(filename1)
            df = f'df{i}'
            glb[df] = pd.read_csv(filename1,sep='none',header=None,engine='python')
            glb[df] = glb[df].iloc[35:]
            glb[df] = glb[df].columns.str.replace("\t"," ")           #remove \t and insert space
            glb[df] = glb[df].columns.str.split(expand=True)          #split into 2 columns
            i+=1

部分输出:行glb[df] = glb[df].iloc[35:]产生

                                0
35   0.000000e+000\t5.718826e-005
36   5.000000e+000\t5.039635e-004
..
..
..
..

期望输出:每个 df{i} 中的 2 列

                 x            y
35   0.000000e+000 5.718826e-005
36   5.000000e+000 5.039635e-004

0 个答案:

没有答案