如何简单地将单元格从一个csv“矩阵”映射到另一个csv矩阵,数据帧保留行/列ord

2024-10-01 15:47:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我想创建一个csv文件,将一个csv文件复制到另一个csv文件,替换原始csv文件中的值,同时使用其他文件中的不同“标题名称键”映射列,并替换原始csv文件中的列。每个文件的行数相同。行顺序需要从输入到输出进行维护,列顺序需要按照更改原始文件的顺序进行维护。在C++类java伪代码中我想做的是:

newValues = pandas.read_csv('newValues.csv')
sourceMatrix = pandas.read_csv('sourceFile.csv')

# note index is positional from row 0 to max

for( rowIndex = 0; rowIndex < rowCount; ++rowIndex) {
   newRow = newValues.rows[rowIndex];
   destRow = sourceMatrix.rows[rowIndex];
   # modify original row columns "sparsely" with other "newValues" columns
   destRow['destKey1'] = newValues['sourceKey1'] 
   ...
   sourceMatrix.rows[rowIndex] = destRow # replace original row with changed row
}

# write output of altered source 
sourceMatrix.to_csv('outputFile.csv')

在部分和几乎不被描述的上下文中有很多比特和片段,但当所有内容都是动态查找并且可能不保留键/值顺序时,不容易探索如何执行上述操作。在这种情况下,数据类型与字段中是否包含字符串无关


Tags: columns文件csvtopandasread顺序with
1条回答
网友
1楼 · 发布于 2024-10-01 15:47:31

好吧,男人很穷

当你还不了解系统的时候,这是一个非常好的方法

https://www.tutorialspoint.com/python_pandas

无论如何,这里有一个第一关是有效的

#!/usr/bin/python3

import os
import sys
import pandas as pd

fileToMapTo = 'SourceValues.csv'
fileToCorrectFrom = './correctedValues.csv'
outputFile = 'SourceValues-corrected.csv'


#  read in the source files
print("mapping \"" + fileToCorrectFrom + "\" over " + fileToMapTo);

try:
    subject_df = pd.read_csv(fileToMapTo) # load with default positional index
    fixer_df = pd.read_csv(fileToCorrectFrom) # load with default positional index
except:
    sys.err.write("Pandas Unable to load: \"" + fileToValidate + "\"\n")
    sys.exit(1)

# create new frame with same column headers as source frame (sets 'x' size of columns)
# and same "index" ( sets Y size of matrix and fills with empty rows )
output_df = pd.DataFrame( index = subject_df.index, columns = subject_df.columns.values );

#iterate over rows (In index order)
for index, row in subject_df.iterrows():
    row['AccNumber'] = 1234  # replace a value for a specific column
    output_df.iloc[index] = row  # set item in output to item in (copy of) input

#write output .csv file from output data frame

# create output folder for results if not present
outputDir = os.path.abspath(os.path.join(outputFile, os.pardir))
if not os.path.isdir(outputDir):
    os.makedirs(outputDir)

print("writing mapped file to: \"" + outputFile + "\"")

# write without (generated integer, ordered) index names
output_df.to_csv(outputFile, index=False)

相关问题 更多 >

    热门问题