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