制作数据集

批量处理信号

把trainX放到一个CSV、trainY放到一个CSV

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
def read_salt_data(path,name):

"""
this function reads csv in batch
path is the folder where the files are located
name is the files name
"""
file=glob.glob(os.path.join(path, name))
for f in file:
dl=pd.read_excel(f,header=None,usecols=[1])
dl=dl.drop(index=[0,1])
print(dl.values)
# dl_befor = dl[:1550]
# dl_after = dl[1551:]
# print(dl_befor.shape)
# dl_befor.plot()
# # plt.xlabel("time")
# # plt.ylabel("value")
# # plt.grid(True)
# # dl.plot()

path='/Users/squareface/Downloads/salt_data'
name="*.xlsx"
df = read_salt_data(path,name)

遇到问题:

现在可以把一个文件夹里的csv都存储到dl里面,可以把刺激前和刺激后的数据划分,但是无法对在for循环里保存每次读取到的文件

学习代码:

1
2
3
4
5
6
7
8
9
10
list_l = [[1, 3, 3, 5, 4], [11, 7, 15, 13, 9], [4, 2, 7, 9, 3], [15, 11, 12, 6, 11]]
date_range = pd.date_range(start="20180701", periods=4)
df = pd.DataFrame(list_l, index=date_range,
columns=['a', 'b', 'c', 'd', 'e'])
out:
a b c d e
2018-07-01 1 3 3 5 4
2018-07-02 11 7 15 13 9
2018-07-03 4 2 7 9 3
2018-07-04 15 11 12 6 11

这段代码可以把字典保存为CSV文件,并且可以弹出一个可视化界面选择文件保存路径。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import tkinter as tk
from tkinter import filedialog
from pandas import DataFrame
# 制作字典数据
Cars = {'Brand': ['Honda Civic','Toyota Corolla','Ford Focus','Audi A4'],
'Price': [22000,25000,27000,35000]
}
# 字典存为DataFrame格式
df = DataFrame(Cars, columns= ['Brand', 'Price'])


root= tk.Tk()
canvas1 = tk.Canvas(root,
width = 300,
height = 300,
bg = 'lightsteelblue2',
relief = 'raised')
canvas1.pack()

def exportCSV ():
global df
export_file_path = filedialog.asksaveasfilename(defaultextension='.csv')
df.to_csv (export_file_path, index = None, header=True)

saveAsButton_CSV = tk.Button(text='Export CSV',
command=exportCSV,
bg='green',
fg='white',
font=('helvetica', 12, 'bold'))

canvas1.create_window(150, 150, window=saveAsButton_CSV)
root.mainloop()