[英]Add column to several files, combine in one file and sort it
I am new to the Python world, and I am fighting with basic operations. 我是Python世界的新手,我在与基本操作作斗争。 I have a huge amount of files named inlet_10_00_00.csv
, inlet_10_00_01.csv
, inlet_10_00_02.csv
, etc. corresponding to different time inlet_Hour_minute_sec. 我有大量的文件名为inlet_10_00_00.csv
, inlet_10_00_01.csv
, inlet_10_00_02.csv
等, inlet_10_00_02.csv
对应于不同的时间entry_Hour_minute_sec。 The content looks like: 内容如下:
x, y, z, temperature, pressure, u, v, w, k , omega
0, 0, 0, 295, 100001, ...
0, 1, 1, 296, 100002, ...
...
I would like to do the next 2 operations: 我想进行以下2个操作:
1/ Add a t
column to each file with value 0
for inlet_10_00_00
, value 1
for inlet_10_00_01
, etc. Adding a column t
for a single file was quite easy with cols
tools. 1 /向每个文件添加一个t
列,其inlet_10_00_00
值为0
, inlet_10_00_00
值为1
, inlet_10_00_01
。使用cols
工具为单个文件添加一列t
非常容易。 But how can I add it to all files? 但是如何将其添加到所有文件中?
data=pd.read_csv('C:/myPath/inlet_10_00_00.csv',sep=',',skiprows=7)
data["t (s)"]="0"
cols=list(data.columns.values)
cols= cols[0:3]+ cols[-1:] + cols[3:10]
cols
newdata=data[cols]
newdata.to_csv('out.csv', index=False)
2/ I want to combine all these files in one file and sort it by the t
variable. 2 /我想将所有这些文件合并到一个文件中,然后按t
变量对其进行排序。 I should end up with such format: x, y, z, t, temperature,pressure, u, v, w, k , omega 我应该以以下格式结束:x,y,z,t,温度,压力,u,v,w,k,omega
0, 0, 0, 0, 295,100001, .... this part comes from inlet_10_00_00.csv
0, 1, 1, 0, 296,100002, ...
0, 0, 0, 1, 292,100008, ... this part comes from inlet_10_00_01.csv
0, 1, 1, 1, 294,100012, ...
Any ideas, how to proceed? 有什么想法,如何进行?
The following steps should help you achieve the expected result. 以下步骤应有助于您达到预期的结果。
Explanation: 说明:
1. Step 1: Here we'll list the file names in the current directory. 1. 步骤1:在这里,我们将列出当前目录中的文件名。 Then, we'll sort the file names to ensure files are looped in desired sequence. 然后,我们将对文件名进行排序,以确保文件按所需顺序循环播放。
2. Step 2: Here we'll iterate through the list we created in step 1, read a file, add a column and save the data frame with the same name. 2. 步骤2:在这里,我们将遍历在步骤1中创建的列表,读取文件,添加列,并使用相同的名称保存数据框。
3. Step 3: Finally, using pd.concat
we join all the files in a data frame and sort the data frame by t
. 3. 第3步:最后,使用pd.concat
将所有文件pd.concat
到数据帧中,然后按t
对数据帧进行排序。
# Step 1
path = 'C:/myPath/'
files = sorted(os.listdir(path)) # sorting will ensure _001, 002, 003 will bein sequence
## Step 2
for ix, file in enumerate(files,1):
df = pd.read_csv(file, skiprows=7) ## assuming you want to skip first 7 rows in every file
df['t'] = ix
df.to_csv(os.path.join(path, file), index=False)
## Step 3
master_df = pd.concat([pd.read_csv(file) for file in files])
master_df = master_df.sort_values('t').reset_index()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.