我正在尝试为我的项目制作预处理 python 代码。目前,我有多个 csv 文件。我正在尝试执行以下步骤来满足我的愿望:
y
所有 csv 中选择一行命名并合并行(从多个 csv 生成 1 个 csv)。例如。当前 csv(所有 CSV 包含具有 3 列 x、y 和 z 的相似数据)
1.csv
X | 和 | 和 |
---|---|---|
0.001796 | 0.116487 | 0 |
0.003592 | 0.116487 | 0 |
0.005387 | 0.116487 | 0 |
0.007183 | 0.116487 | 0 |
0.008979 | 0.116487 | 0 |
0.010775 | 0.116486 | 0 |
0.012571 | 0.116486 | 0 |
0.014367 | 0.116486 | 0 |
0.016162 | 0.116486 | 0 |
..... | ………… | ... |
2.csv
X | 和 | 和 |
---|---|---|
0.001796 | 0.116 | 0 |
0.003592 | 0.11 | 0 |
0.005387 | 0.1 | 0 |
0.007183 | 0.11 | 0 |
0.008979 | 0.1164 | 0 |
0.010775 | 0.116 | 0 |
0.012571 | 0.1164 | 0 |
0.014367 | 0.116 | 0 |
0.016162 | 0.1164 | 0 |
..... | ………… | ... |
有许多具有几乎相似值的 CSV。
100.csv
X | 和 | 和 |
---|---|---|
0.001796 | 0.091 | 0 |
0.003592 | 0.0930 | 0 |
0.005387 | 0.0931 | 0 |
0.007183 | 0.09355 | 0 |
0.008979 | 0.0955 | 0 |
0.010775 | 0.09 | 0 |
0.012571 | 0.092 | 0 |
0.014367 | 0.0933 | 0 |
0.016162 | 0.0932 | 0 |
..... | ………… | ... |
我想要(在将所有 csv 合并为一个之后):
y1 | y2 | y3 | y4 | y5 | y6 | y7 | y8 | y9 | 类型 |
---|---|---|---|---|---|---|---|---|---|
0.116487 | 0.116487 | 0.116487 | 0.116487 | 0.116487 | 0.116486 | 0.116486 | 0.116486 | 0.116486 | 0 |
0.116 | 0.11 | 0.1 | 0.11 | 0.1164 | 0.116 | 0.1164 | 0.116 | 0.1164 | 0 |
………… | ………… | ………… | ………… | ………… | ………… | ………… | ………… | ………… | 0 |
0.091 | 0.0930 | 0.0931 | 0.09355 | 0.0955 | 0.09 | 0.092 | 0.0933 | 0.0932 | 1 |
………… | ………… | ………… | ………… | ………… | ………… | ………… | ………… | ………… | 1 |
………… | ………… | ………… | ………… | ………… | ………… | ………… | ………… | ………… | 1 |
在最后一列,我想1
在 50 或 100 行之后添加。
这是到目前为止我所做的:
from glob import glob
from natsort import natsorted
import pandas as pd
import os
import csv
from csv import reader, writer
import shutils
from glob import glob
from natsort import natsorted
files = glob('./a_csv/*.csv')
save_path = "./data"
if not os.path.exists(save_path):
os.mkdir(save_path)
#combined all csv row wise with y columns and transpose
def read_2nd(fn):
return pd.read_csv(fn, delim_whitespace=1, usecols=[1])
big_df = pd.concat([read_2nd(fn) for fn in natsorted(files)], axis=1)
df = big_df.T #Transpose the data
#add_header
header = []
for i in range(0, 120):
headers = "z_" + str(i)
i += 1
header.append(headers)
type_head = "type"
header += [type_head]
#print(header)
df = df.iloc[:, :120] #csv is large, I want to choose only 120 columns
print(len(df)) # output is 200
for i in range(len(df)): # I want to divide rows into 100 and 100
if i <= 100: # for less than 100 I want to add 0
df.insert(120, column = "type", value = "0")
else: #for remaining I want to add 1
df.insert(120, column = "type", value = "1")
df.to_csv('./data/final.csv', header=header, index=False) #After adding I want to save csv as final.csv
在 for 循环之前,它可以按我的意愿工作,但不会按我的意愿添加新列。
在转置之前,多个 CSV 有超过 10k 行。所有 CSV 都具有相同的长度。
转置后我希望有 200 行。由于行是转置的,列的 len 变得超过 10k,所以我在代码中最多只选择了 120。
所以预期的 csv 将有 200 行和 120 列。
任何帮助或建议将不胜感激。谢谢
Step 1: make a reproducible example.
files = [f'/tmp/foo_{i:03d}.csv' for i in range(300)]
for filename in files:
pd.DataFrame(
np.random.uniform(size=(200, 3)),
columns=list('xyz')
).to_csv(filename, index=False)
Step 2: Solution
# read all files, select first n_values of column y and concatenate as rows
n_values = 4 # change to the number of columns desired in output
df = pd.concat([
pd.read_csv(filename).head(n_values)[['y']].T
for filename in files
]).reset_index(drop=True)
# change column names 0 --> y1, 1 --> y2, etc.
df.columns = [f'y{c+1}' for c in df.columns]
# add a column 'type' with value 0 for first 100 rows, then 1 for next 100, etc.
df['type'] = df.index // 100
# result
>>> df
y1 y2 y3 y4 type
0 0.526375 0.984637 0.684822 0.621827 0
1 0.483059 0.451609 0.466958 0.810819 0
2 0.459988 0.215904 0.925931 0.520551 0
3 0.559822 0.847502 0.382065 0.371135 0
4 0.465607 0.621670 0.670426 0.266533 0
.. ... ... ... ... ...
295 0.865073 0.472095 0.579716 0.499318 2
296 0.202211 0.440066 0.546456 0.218273 2
297 0.265703 0.416152 0.847737 0.342023 2
298 0.569874 0.634658 0.774765 0.521240 2
299 0.010179 0.148335 0.917785 0.927565 2
If instead, the column type
should be 0 for the first 100 rows, then 1 ever after:
df['type'] = (df.index >= 100).astype(int)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句