Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read multiple csv files in a folder

Tags:

python

pandas

I have multiple .csv files that represents a serious of measurements maiden.

I need to plot them in order to compare proceeding alterations.

I basically want to create a function with it I can read the file into a list and replay several of the "data cleaning in each .csv file" Then plot them all together in a happy graph

this is a task I need to do to analyze some results. I intend to make this in python/pandas as I might need to integrate into a bigger picture in the future but for now, this is it.

I basically want to create a function with it I can read the file into a big picture comparing it Graph.

I also have one file that represents background noise. I want to remove these values from the others .csv as well

import matplotlib.pyplot as plt
from matplotlib.ticker import FormatStrFormatter
PATH = r'C:\Users\UserName\Documents\FSC\Folder_name'
FileNames = [os.listdir(PATH)]
for files in FileNames:
    df = pd.read_csv(PATH + file, index_col = 0)

I expected to read every file and store into this List but I got this code:

FileNotFoundError: [Errno 2] File b'C:\Users\UserName\Documents\FSC\FolderNameFileName.csv' does not exist: b'C:\Users\UserName\Documents\FSC\FolderNameFileName.csv'

like image 670
dellaklo Avatar asked Jun 08 '26 03:06

dellaklo


1 Answers

Have you used pathlib from the standard library? it makes working with the file system a breeze,

recommend reading : https://realpython.com/python-pathlib/

try:

from pathlib import Path
files = Path('/your/path/here/').glob('*.csv') # get all csvs in your dir.
for file in files:
   df = pd.read_csv(file,index_col = 0)
   # your plots.
like image 85
Umar.H Avatar answered Jun 10 '26 19:06

Umar.H



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!