Home > Mobile >  Is there a way to read and plot the nth file in a folder in python?
Is there a way to read and plot the nth file in a folder in python?

Time:02-01

Im trying to find a quick and easy way to read and plot the nth csv file in a folder,

im currently working with the following, to read all files in the folder>

import os
import glob
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
  
  
path = os.getcwd()
csv_files = glob.glob(os.path.join(path, "*.csv"))  
  
for file in csv_files:
      
    # read adn plot the csv file
    Data = pd.read_csv(file,header=33)
    sns.lineplot(x=Data['x'],y=Data['y'],data=Data)

but is there a way to read and plot every 4th file for example?

CodePudding user response:

for file, count in enumerate(csv_files, start=1):
       if count % 4:
            Data = pd.read_csv(file,header=33)
            sns.lineplot(x=Data['x'],y=Data['y'],data=Data)

count will keep increasing and only read every 4th file.

CodePudding user response:

You can use a counter variable (which is a very beginner approach, but easy to start).

counter = 0
for file in csv_files:
    # Increase the counter value for each iteration
    counter = counter  1  
    # read adn plot the csv file
    Data = pd.read_csv(file,header=33)
    # For example, you want to print the third file
    if counter == 3:
        sns.lineplot(x=Data['x'],y=Data['y'],data=Data)
  •  Tags:  
  • Related