简体   繁体   English

使用多个.csv文件在python中自动制作多个图

[英]Automate making multiple plots in python using several .csv files

I have 14 .csv files (1 .csv file per location) that will be used to make a 14 bar plots of daily rainfall. 我有14个.csv文件(每个位置1个.csv文件),这些文件将用于制作14条每日降雨的条形图。 The following code is an example of what one bar plot will look like. 以下代码是一个条形图的示例。

import numpy as np
import pandas as pd 
from datetime import datetime, time, date
import matplotlib.pyplot as plt

# Import data
dat = pd.read_csv('a.csv')
df0 = dat.loc[:, ['TimeStamp', 'RF']]

# Change time format
df0["time"] = pd.to_datetime(df0["TimeStamp"])
df0["day"] = df0['time'].map(lambda x: x.day)
df0["month"] = df0['time'].map(lambda x: x.month)
df0["year"] = df0['time'].map(lambda x: x.year)
df0.to_csv("a2.csv", na_rep="0")  # write to csv

# Combine for daily rainfall
df1 = pd.read_csv('a2.csv', encoding='latin-1',
              usecols=['day', 'month', 'year', 'RF', 'TimeStamp'])
df2 = df1.groupby(['day', 'month', 'year'], as_index=False).sum()
df2.to_csv("a3.csv", na_rep="0", header=None)  # write to csv

# parse date
df3 = pd.read_csv("a3.csv", header=None, index_col='datetime', 
             parse_dates={'datetime': [1,2,3]}, 
             date_parser=lambda x: pd.datetime.strptime(x, '%d %m %Y'))

def dt_parse(date_string):
dt = pd.datetime.strptime(date_string, '%d %m %Y')
return dt

# sort datetime
df4 = df3.sort()
final = df4.reset_index()

# rename columns
final.columns = ['date', 'bleh', 'rf']

final[['date','rf']].plot()

plt.suptitle('Rain 2015-2016', fontsize=20)
plt.xlabel('Date', fontsize=18)
plt.ylabel('Rain / mm', fontsize=16)
plt.savefig('a.jpg')
plt.show()

And the final plot looks like this: 最后的情节看起来像这样: 在此处输入图片说明

How can I automate this code (ie write a for-loop perhaps?) so that I don't have to re-type the code for each .csv file? 如何自动执行此代码(例如,可能编写for循环?),而不必为每个.csv文件重新键入代码? It would be nice if the code also saves the figure with the name of the .csv as the name of the .jpg file. 如果代码还将图形保存为.csv作为.jpg文件的名称,那将是很好的。

The names of the 14 files are as such: names = ["a.csv","b.csv", "c.csv","d.csv","e.csv","f.csv"...] 14个文件的名称如下:names = [“ a.csv”,“ b.csv”,“ c.csv”,“ d.csv”,“ e.csv”,“ f.csv”。 。]

Here's an example of the type of file that I'm working with: https://dl.dropboxusercontent.com/u/45095175/test.csv 这是我正在使用的文件类型的示例: https : //dl.dropboxusercontent.com/u/45095175/test.csv

First method: you need to put all your csv files in the current folder. 第一种方法:您需要将所有csv文件放入当前文件夹中。 You also need to use the os module. 您还需要使用os模块。

import os
for f in os.listdir('.'):                 # loop through all the files in your current folder
    if f.endswith('.csv'):                # find csv files
        fn, fext = os.path.splitext(f)    # split file name and extension

        dat = pd.read_csv(f)              # import data
        # Run the rest of your code here

        plt.savefig('{}.jpg'.format(fn))  # name the figure with the same file name 

Second method: if you don't want to use the os module, you can put your file names in a list like this: 第二种方法:如果您不想使用os模块,则可以将文件名放在这样的列表中:

files = ['a.csv', 'b.csv']

for f in files:
    fn = f.split('.')[0]

    dat = pd.read_csv(f)
    # Run the rest of your code here

    plt.savefig('{}.jpg'.format(fn))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM