0

I am working on a project regarding CSV files. I barely at the start of learning python and its mysteries. I have this piece of code that Looks in a folder, gets all ".csv" files (or ".txt" in this case, this is just how I found all the massive data) and reads out its data. After I import the CSV with pandas ( it has 2 columns, Time and Amplitude) i want to plot the two columns. I know how the plot looks like already ( the data was plotted in MATLAB but my job is to create a software for python). I tried using this link but I do not know how to make it know that my X-axis is Time and the Y-axis is Amplitude

this is my whole code so far

import pandas as pd
import os
import tkinter as tk 
from tkinter.filedialog import askdirectory 
import matplotlib.pyplot as plt


print ("Please choose the path to the CSV Files: ")


root = tk.Tk()
root.withdraw()
root.attributes('-topmost', True)
path = askdirectory()

print("\nThe chosen folder is: " + path)

tmp_paths=[] 
data_tables=[] 
data_paths=[] 
file_ext=[] 
file_name=[]
sep_name=[] 




for root, dirs, files in os.walk(path):     
    for file in files:                      
        if file.endswith(".txt"):          
             tmp_paths=os.path.join(root,file)  #
             tables=pd.read_csv(tmp_paths, header=5, sep=' ',converters={"Time":float, "Ampl":float}) 
             data_tables.append(tables)     
             data_paths.append(tmp_paths)      
                           

file_ext=[name.split("\\",1)[1] for name in data_paths] 
file_name=[name.split(".txt",1)[0] for name in file_ext]
sep_name=[name.split("-",6) for name in file_name]

plt.plot(data_tables[1],data_tables[0])
plt.show()

P.S: When I try to plot it gives me: `KeyError: 'Time'.

EDIT

1.converted the data_tables values to float using pandas

2.fixed ecopy error in the plot code

After the new Edit I no longer get the KeyError but Time and Ampl give another error saying that the indices are expected to be integers not str or slices and the only values it accepts are 1 or 0, any other value is out of index

Community
  • 1
  • 1
Maiels
  • 47
  • 1
  • 11
  • Sounds like your `tables` dict has no key "Time". Do all the files in the directory that are getting parsed have a "Time" column? Do the file column names get passed to `tables` as dict keys? – stephenlechner May 07 '17 at 18:21
  • @stephenlechner `tables` isn't a `dict`, it is a `pandas.DataFrame`. – juanpa.arrivillaga May 07 '17 at 18:25
  • 1
    Isn't `tables` meant to be just a temporary variable in the loop? I would expect you to use one of the tables in `data_tables` for plotting. By the way, you can simplify a lot of the path handling by using `pathlib.Path`. For example, that loop could be replaced with just `for file in Path().glob('**/*.txt'):`. – Martin Valgur May 07 '17 at 19:26
  • May I see the output of ` table.info()` – stovfl May 08 '17 at 12:15
  • @stovfl this is what `table.info()` shows: ` RangeIndex: 1000 entries, 0 to 999 Data columns (total 2 columns): -2.50301e-006 1000 non-null float64 -69.7396 1000 non-null float64 dtypes: float64(2) memory usage: 15.7 KB` – Maiels May 09 '17 at 22:07
  • @MartinValgur I just noticed that, I am sorry, I will edit and indeed tables in temporary. Anyway After the edit I noticed that I ineed got them to be Float, using the in-built `pandas` converter but the only `range` i could choose was between `1` and `0` , I can`t choose the `data_tables` size - 57 or its values. A graph IS Shown but I do not know if the `1` and `0` actually mean the column number or the actual rows. Do you know any links to some documentation? Thank you! – Maiels May 09 '17 at 22:13
  • @MartinValgur i will take using `pathlib.Path` into consideration, upon further documentation it sounds much easier to use, but I want to do this after I get more of the code done, when I will have to "optimize" it. – Maiels May 09 '17 at 22:20

1 Answers1

0

Regarding to the pandas Documentation you have plot a DataFrame like that:
To plot all of the columns with labels

plt.figure()
data_tables.plot()
plt.legend(loc='best')

pandas-docs/version/0.13.1/visualization

stovfl
  • 14,998
  • 7
  • 24
  • 51