在csv的同一列中添加两个不同的值

代码狂

我正在尝试为我的项目制作预处理 python 代码。目前,我有多个 csv 文件。我正在尝试执行以下步骤来满足我的愿望:

  1. 仅从y所有 csv 中选择一行命名并合并行(从多个 csv 生成 1 个 csv)。
  2. 转置整个 csv 数据。
  3. 给标题
  4. 最后在列的末尾再添加一列并添加“0”最多 100 行,并在 100 行(剩余)后添加“1”。

例如。当前 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 列。

任何帮助或建议将不胜感激。谢谢

彼得·D

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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何计算同一列中的两个不同的列值?

选择在同一列中具有两个不同值的行

如何从 SQL 连接中的同一列返回两个不同的值?

如何在 MySQL 中获取同一列的两个不同值

同一列中的两个值

比较同一列中的两个值

如何从R中的两个不同数据帧中添加两列,其中一列只是另一列的唯一值的子集

从同一查询的同一列中获取两个值

如何将来自同一列但位于不同行的两个值相除

如何从同一张表中查询同一列但条件不同的两个不同的总和?

如何从表中的同一列“计数”两个不同的条件?

如何从同一列中的两个不同日期输出前 5 名?

如何基于单独的日期列划分同一列中的两个值

在mysql的两个表中基于同一列对两个列值进行计数

在同一列表中添加两个对象

匹配 Excel 中同一列中两个日期之间的所有值

如何将两个表中的值包含在同一列中

如何选择一列中两个不同列中可用的值列表?

将同一列中的两个特殊值相除

熊猫-合并/连接同一列中的两个值

比较 SQL Server 中同一列的两个值

检查同一列中是否存在两个对应的值

SQL-如何在一列和两个结果中获取两个不同列的值?

BO WEBI:在两个不同的列中从同一维提取不同的值

比较两个不同数据框中的字符串并添加一列

在同一列中查询3个不同的值

一列中两个不同条件的总和

在一列中连接具有不同值的两个表

为什么在 oracle SQL 中,在 where 条件差异很大的情况下,对同一列执行具有两个不同值的查询所花费的时间