pandas 如何在 Python 中读取和写入 CSV 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44873342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read and write to CSV Files in Python
提问by user3476378
I have a csv file, which has only a single column , which acts as my input.
我有一个 csv 文件,它只有一列,作为我的输入。
I use that input to find my outputs. I have multiple outputs and I need those outputs in another csv file.
我使用该输入来查找我的输出。我有多个输出,我需要另一个 csv 文件中的这些输出。
Can anyone please suggest me the ways on how to do it ?
任何人都可以建议我如何做吗?
Here is the code :
这是代码:
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
回答by seralouk
Here is a simple example:
这是一个简单的例子:
The data.csv is a csv with one column and multiple rows.
data.csv 是一列多行的 csv。
The results.csv contain the mean and median of the input and is a csv with 1 row and 2 columns (mean is in 1st column and median in 2nd column)
results.csv 包含输入的平均值和中位数,是一个 1 行 2 列的 csv(平均值在第一列,中位数在第二列)
Example:
例子:
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)
回答by seralouk
In pseudo code, you'll do something like this:
在伪代码中,您将执行以下操作:
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
Now, I won't write a full-working example because it's better for you to start digging and ask specificquestions when you have some. You're now just asking: write this for me because I don't know Python.
现在,我不会写一个完整的示例,因为当你有一些问题时,最好开始挖掘并提出具体问题。你现在只是问:为我写这个,因为我不知道 Python。
回答by jezrael
I think you need read_csv
for reading file to Series
and to_csv
for writing output Series
to file in looping by Series.iteritems
.
我认为您需要在循环中read_csv
读取文件Series
并将to_csv
输出写入Series
文件Series.iteritems
。
#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