Skip to content
Advertisement

Is there a way that with a for cicle I could run through the archives in a file in jupyter lab/python?

I’m doing an inform and with a group we run the experiment ten time and had created 10 csv files. But opening all one line of code at a time must not be the most efficient way to open and read them and I know there must be a way to open them with a for cicle.

For now I have this code,changing cn1 with cn2 and so on:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

cn1 = pd.read_csv ("Cuerpo Negro 1 Volt.txt",skiprows=1)

datos1 =pd.DataFrame (cn1)

What is the way to, with less lines, to open them all?

I have all the archive in the same file I’m working

Thanks for your help

Ps:I have plt because I need to graphic them later, but there must not be a problem to repeat the same code for use to transform the csv to a data frame.

Advertisement

Answer

If I understood you correctly what you want is a list with all your pandas DataFrames from your csv file. So frames is a list in which step by step all the DataFrames df are appended to. They are accessible by frames[index]

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

number_of_files = 10
frames = []
filename = "your_file{number}.csv" # In your case: "Cuerpo Negro {number} Volt.txt"

for i in range(1, number_of_files+1):
    df = pd.read_csv(filename.format(number=i), skiprows=1)
    frames.append(df)
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement