我有一个csv文件,它只有一列,作为我的输入。
我使用该输入来查找输出。我有多个输出,我需要在另一个csv文件中的那些输出。
任何人都可以建议我如何做到这一点?
以下是代码:
import urllib.request
jd = {input 1}
//
Some Codes to find output - a,b,c,d,e
//
** Code to write output to a csv file.
** Repeat the code with next input of input csv file.
Input CSV File has only a single column and is represented below:
1
2
3
4
5
Output would in a separate csv in a given below format :
It would be in multiple rows and multiple columns format.
a b c d e
答案 0 :(得分:2)
这是一个简单的例子:
data.csv是一个包含一列和多行的csv。
results.csv包含输入的平均值和中位数,是一个包含1行和2列的csv(平均值在第1列,中位数在第2列)
示例:
import numpy as np
import pandas as pd
import csv
#load the data
data = pd.read_csv("data.csv", header=None)
#calculate things for the 1st column that has the data
calculate_mean = [np.mean(data.loc[:,0])]
calculate_median = [np.median(data.loc[:,0])]
results = [calculate_mean, calculate_median]
#write results to csv
row = []
for result in results:
row.append(result)
with open("results.csv", "wb") as file:
writer = csv.writer(file)
writer.writerow(row)
答案 1 :(得分:1)
我认为您需要read_csv
才能将文件读取到Series
和to_csv
,以便在Series.iteritems
的循环中将输出Series
写入文件。
#file content
1
3
5
s = pd.read_csv('file', squeeze=True, names=['a'])
print (s)
0 1
1 3
2 5
Name: a, dtype: int64
for i, val in s.iteritems():
#print (val)
#some operation with scalar value val
df = pd.DataFrame({'a':np.arange(val)})
df['a'] = df['a'] * 10
print (df)
#write to csv, file name by val
df.to_csv(str(val) + '.csv', index=False)
a
0 0
a
0 0
1 10
2 20
a
0 0
1 10
2 20
3 30
4 40
答案 2 :(得分:1)
在伪代码中,你会做这样的事情:
for each_file in a_folder_that_contains_csv: # go through all the `inputs` - csv files
with open(each_file) as csv_file, open(other_file) as output_file: # open each csv file, and a new csv file
process_the_input_from_each_csv # process the data you read from the csv_file
export_to_output_file # export the data to the new csv file
现在,我不会写一个完整的例子,因为当你有一些问题时,你最好开始挖掘并询问具体的问题。你现在只是问:为我写这个,因为我不懂Python 。