Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading