我有一个包含多个txt文件的文件夹,其中包含不同格式的数据,我稍后会合并。暂时我需要以相同的方式对它们进行全部格式化。
path =r'C:\\Users\\data'
filenames = glob.glob(path + "/*.txt")
dfs = []
for filename in filenames:
dfs.append(pd.read_csv(filename, infer_datetime_format=True, sep=None,
engine='python', header=None))
然后我对列表中的几个数据帧有一个特定的格式:
#Format 1
dfs[3][[0,1]]= dfs[3][1].str.split(expand=True)
dfs[4][[0,1]]= dfs[4][1].str.split(expand=True)
dfs[3].columns=['Date','Time','Wind Vel 1','Wind Vel 2','Dir1','Dir2','NaN']
dfs[3] = dfs[3].drop('NaN',1)
dfs[4].columns=['Date','Time','Wind Vel 1','Wind Vel 2','Dir1','Dir2','NaN']
dfs[4] = dfs[4].drop('NaN',1)
如何在循环中为dfs [3]和dfs [4]完成此操作?
答案 0 :(得分:0)
轻松:
for i in (3,4):
dfs[i][[0,1]]= dfs[i][1].str.split(expand=True)
dfs[i].columns=['Date','Time','Wind Vel 1','Wind Vel 2','Dir1','Dir2','NaN']
dfs[i] = dfs[i].drop('NaN',1)