我试图从12个.txt文件的目录中提取数据。每个文件包含我想要提取的3列数据(X,Y,Z)。我想收集一个df(InforDF)中的所有数据,但到目前为止,我只能成功创建一个df,其中包含同一列中的所有X,Y和Z数据。这是我的代码:使用python从多个文件中提取数据
import pandas as pd
import numpy as np
import os
import fnmatch
path = os.getcwd()
file_list = os.listdir(path)
InfoDF = pd.DataFrame()
for file in file_list:
try:
if fnmatch.fnmatch(file, '*.txt'):
filedata = open(file, 'r')
df = pd.read_table(filedata, delim_whitespace=True, names={'X','Y','Z'})
except Exception as e:
print(e)
我在做什么错?
作为的话,不抓,一般例外(总是抓特殊类型的例外) –
您在每次迭代时覆盖df –