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

7
  • 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? Commented May 7, 2017 at 18:21
  • @stephenlechner tables isn't a dict, it is a pandas.DataFrame. Commented May 7, 2017 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'):. Commented May 7, 2017 at 19:26
  • May I see the output of ` table.info()` Commented May 8, 2017 at 12:15
  • @stovfl this is what table.info() shows: <class 'pandas.core.frame.DataFrame'> 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 Commented May 9, 2017 at 22:07

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.