阅读多个CSV文件Python Pandas

时间:2014-10-15 07:31:58

标签: python csv matplotlib pandas

我尝试在目录中读取多个具有不同名称的CSV文件(例如:Hello.csv,World.csv,...)。现在我需要将绘图图像保存在不同的目录中。这是我的工作

import matplotlib
import pandas as pd
import os as operating
import matplotlib.pyplot as plt

#fonction to read files
def read_file(filename):
    df = pd.read_csv(filename , header=None, delim_whitespace=True, parse_dates={'datetime':[0,1]}, index_col='datetime', usecols=[0,1,2])
    df_New = df.rename(columns={2:'debit'})
    df_New_New = df_New.query('debit > 0')
    df_New_New.plot()

#change the directory path
operating.chdir('/home/user/Documents/try_folder/')

#list files name in a variable files_in_path
files_in_path = operating.listdir('/home/user/Documents/try_folder/')

在Ipython中,这是输出

In [6]: files_in_path = operating.listdir('/home/user/Documents/try_folder/')
In [7]: files_in_path
Out[7]:
['satro10.txt',
 'satro6.txt',
 'satro3.txt',
 'satro.txt',
 'satro9.txt',
 'satro7.txt',
 'satro2.txt',
 'satro8.txt',
 'satro4.txt',
 'satro5.txt']

现在我需要用

保存剧情的png图像文件
for i in files_in_path:
    read_file(i)
     plt.savefig(operating.path.splitext(operating.path.basename(i))[0] + '.png')

如何将png保存在不同的目录路径中? 感谢名单

0 个答案:

没有答案