使用几个.csv文件自动在python中创建多个图

时间:2021-06-17 20:30:52

I have 14 .csv files (1 .csv file per location) that will be used to make a 14 bar plots of daily rainfall. The following code is an example of what one bar plot will look like.

我有14个.csv文件(每个位置1个.csv文件),用于制作14巴的每日降雨量。以下代码是一个条形图的外观示例。

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: 使用几个.csv文件自动在python中创建多个图

最后的情节看起来像这样:

How can I automate this code (i.e. write a for-loop perhaps?) so that I don't have to re-type the code for each .csv file? It would be nice if the code also saves the figure with the name of the .csv as the name of the .jpg file.

我怎样才能自动化这段代码(也就是写一个for循环?),这样我就不必为每个.csv文件重新输入代码了?如果代码还保存带有.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

1 个解决方案

#1


1  

First method: you need to put all your csv files in the current folder. You also need to use the os module.

第一种方法:您需要将所有csv文件放在当前文件夹中。您还需要使用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))

#1


1  

First method: you need to put all your csv files in the current folder. You also need to use the os module.

第一种方法:您需要将所有csv文件放在当前文件夹中。您还需要使用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))