スポンサーリンク
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 |
import csv import pandas as pd from datetime import datetime import os import glob os.chdir("C://sample5") def process_csv(csv_file): data = [] with open(csv_file, 'r') as file: reader = csv.reader(file) next(reader) # 最初の行をスキップ header = next(reader) # 2番目の行をヘッダーとして読み込む for row in reader: data.append(row) df = pd.DataFrame(data, columns=header) df["データ日時"] = pd.to_datetime(df["データ日時"]) df = df.set_index("データ日時") df["積算発電"] = pd.to_numeric(df["積算発電"]) df["元データ日時"] = df.index.to_series() df = df.groupby([df.index.date, df.index.hour]).last() df['時間差分'] = df['元データ日時'].diff() df['時間差分'] = df['時間差分'].shift(-1) df['積算発電差分'] = df['積算発電'].diff() df['積算発電差分'] = df['積算発電差分'].shift(-1) return df # フォルダ内のすべてのCSVファイルに対して処理を適用します csv_files = glob.glob("*.csv") for csv_file in csv_files: df = process_csv(csv_file) print(f"{csv_file} の処理が完了しました。") import csv csv_file = 'sample.csv' # 結果を格納するリスト result = [] with open(csv_file, 'r') as file: reader = csv.reader(file) # ヘッダー行を読み込む header = next(reader) result.append(header) # データ行を読み込む for row in reader: result.append(row) # 結果を表示 for row in result: print(row) import csv csv_file = 'sample.csv' # 結果を格納するリスト result = [] with open(csv_file, 'r') as file: reader = csv.reader(file) # ヘッダー行を読み込む header = next(reader) result.append(header) # データ行を読み込む for row in reader: result.append(row) # 結果を表示 for row in result: print(row) import csv from collections import OrderedDict csv_file = 'sample.csv' # インデックス列の名前と通常列の名前 index_column = 'timestamp' data_column = 'generation' # 結果を格納するOrderedDict result = OrderedDict() with open(csv_file, 'r') as file: reader = csv.DictReader(file) for row in reader: index_value = row[index_column] data_value = row[data_column] result[index_value] = data_value # 結果を表示 for index, value in result.items(): print(f"{index}: {value}") import tkinter as tk from tkinter import ttk import pandas as pd import matplotlib.pyplot as plt from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg from tkinter import filedialog root = tk.Tk() root.title("CSV Plotter") # Create a main frame frame_main = ttk.Frame(root) frame_main.pack(fill="both", expand=True) # Create a canvas and attach it to the main frame canvas = tk.Canvas(frame_main) canvas.pack(side="left", fill="both", expand=True) # Create horizontal and vertical scrollbars x_scrollbar = ttk.Scrollbar(frame_main, orient="horizontal", command=canvas.xview) x_scrollbar.pack(side="bottom", fill="x") y_scrollbar = ttk.Scrollbar(frame_main, orient="vertical", command=canvas.yview) y_scrollbar.pack(side="right", fill="y") canvas.configure(xscrollcommand=x_scrollbar.set, yscrollcommand=y_scrollbar.set) # Create a subframe and attach it to the canvas frame_sub = ttk.Frame(canvas) canvas.create_window((0, 0), window=frame_sub, anchor="nw") # Rest of the code # ... def on_configure(event): canvas.configure(scrollregion=canvas.bbox("all")) frame_sub.bind('<Configure>', on_configure) root.mainloop() import tkinter as tk import pandas as pd import matplotlib.pyplot as plt from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg from tkinter import filedialog root = tk.Tk() root.title("CSV Plotter") # Create a main canvas and attach scrollbars main_canvas = tk.Canvas(root) main_canvas.pack(side="left", fill="both", expand=True) x_scrollbar = tk.Scrollbar(root, orient="horizontal", command=main_canvas.xview) x_scrollbar.pack(side="bottom", fill="x") y_scrollbar = tk.Scrollbar(root, orient="vertical", command=main_canvas.yview) y_scrollbar.pack(side="right", fill="y") main_canvas.configure(xscrollcommand=x_scrollbar.set, yscrollcommand=y_scrollbar.set) # Create a main frame and attach it to the main canvas frame_main = tk.Frame(main_canvas, bg="white") main_canvas.create_window((0, 0), window=frame_main, anchor="nw") # Create two canvases for Y-axis 1 and Y-axis 2 fig_y_axis1, ax1 = plt.subplots(nrows=3, ncols=1, figsize=(6, 9)) canvas_y_axis1 = FigureCanvasTkAgg(fig_y_axis1, master=frame_main) canvas_y_axis1.get_tk_widget().place(x=10, y=150, width=500, height=450) fig_y_axis2, ax2 = plt.subplots(nrows=3, ncols=1, figsize=(6, 6)) canvas_y_axis2 = FigureCanvasTkAgg(fig_y_axis2, master=frame_main) canvas_y_axis2.get_tk_widget().place(x=520, y=150, width=500, height=450) # Rest of the code # ... def on_configure(event): main_canvas.configure(scrollregion=main_canvas.bbox("all")) frame_main.bind('<Configure>', on_configure) root.mainloop() import tkinter as tk import pandas as pd import matplotlib.pyplot as plt from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg from tkinter import filedialog root = tk.Tk() root.title("CSV Plotter") root.geometry("1000x800") # Create a canvas to hold all widgets main_canvas = tk.Canvas(root, bg="white") main_canvas.pack(side="left", fill="both", expand=True) # Create two scrollbars scrollbar_y = tk.Scrollbar(root, orient='vertical', command=main_canvas.yview) scrollbar_y.pack(side='left', fill='y') scrollbar_x = tk.Scrollbar(root, orient='horizontal', command=main_canvas.xview) scrollbar_x.pack(side='bottom', fill='x') main_canvas.configure(yscrollcommand=scrollbar_y.set, xscrollcommand=scrollbar_x.set) # Create a frame to hold all widgets frame_main = tk.Frame(main_canvas) main_canvas.create_window((0, 0), window=frame_main, anchor="nw") # Create two canvases for Y-axis 1 and Y-axis 2 fig_y_axis1, ax1 = plt.subplots(nrows=3, ncols=1, figsize=(6, 9)) canvas_y_axis1 = FigureCanvasTkAgg(fig_y_axis1, master=frame_main) canvas_y_axis1.get_tk_widget().place(x=10, y=150, width=500, height=450) fig_y_axis2, ax2 = plt.subplots(nrows=3, ncols=1, figsize=(6, 6)) canvas_y_axis2 = FigureCanvasTkAgg(fig_y_axis2, master=frame_main) canvas_y_axis2.get_tk_widget().place(x=520, y=150, width=500, height=450) def clear_plots(): for ax in ax1: ax.clear() for ax in ax2: ax.clear() fig_y_axis1.tight_layout() fig_y_axis2.tight_layout() canvas_y_axis1.draw() canvas_y_axis2.draw() def load_csv_and_plot(): # ... (省略) # グラフのクリアボタンの作成 clear_button = tk.Button(frame_main, text="クリア", command=clear_plots) clear_button.place(x=850, y=10, width=150, height=50) # csv名称ボックス label_file = tk.Label(frame_main, text="CSVファイル名 .csvまで入れる") label_file.place(x=50, y=10) entry_file = tk.Entry(frame_main, width=100) entry_file.place(x=10, y=30, width=200, height=30) # 期間開始ボックス label_start = tk.Label(frame_main, text="Start Date") label_start.place(x=300, y=10) entry_start = tk.Entry(frame_main, width=100) entry_start.place(x=220, y=30, width=200, height=30) # 期間終了ボックス label_end = tk.Label(frame_main, text="End Date") label_end.place(x=500, y=10) entry_end = tk.Entry(frame_main, width=100) entry_end.place(x=420, y=30, width=200, height=30) # Y軸1 ボックス label_y # Y軸1 ボックス label_y_axis = tk.Label(frame_main, text="Y軸の指定1") label_y_axis.place(x=60, y=80) entry_y_axis = tk.Entry(frame_main, width=100) entry_y_axis.place(x=10, y=100, width=200, height=30) # Y軸2 ボックス label_y_axis_additional = tk.Label(frame_main, text="Y軸2の指定2") label_y_axis_additional.place(x=550, y=80) entry_y_axis_additional = tk.Entry(frame_main, width=100) entry_y_axis_additional.place(x=550, y=100, width=200, height=30) # 実行ボタン plot_button = tk.Button(frame_main, text="実行", command=load_csv_and_plot) plot_button.place(x=700, y=10, width=150, height=50) error_label = tk.Label(frame_main, text="") error_label.place(x=750, y=10) # Create labels for data count and mean values data_count_label = tk.Label(frame_main, text="") data_count_label.place(x=50, y=630) weekday_mean_value_label = tk.Label(frame_main, text="") weekday_mean_value_label.place(x=150, y=630) hourly_mean_value_label = tk.Label(frame_main, text="") hourly_mean_value_label.place(x=390, y=630) weekday_mean_additional_value_label = tk.Label(frame_main, text="") weekday_mean_additional_value_label.place(x=580, y=630) hourly_mean_additional_value_label = tk.Label(frame_main, text="") hourly_mean_additional_value_label.place(x=820, y=630) def on_configure(event): main_canvas.configure(scrollregion=main_canvas.bbox("all")) frame_main.bind('<Configure>', on_configure) root.mainloop() # Create a canvas to hold all widgets main_canvas = tk.Canvas(root, bg="white") main_canvas.pack(side="left", fill="both", expand=True) # Create two scrollbars scrollbar_y = tk.Scrollbar(root, orient='vertical', command=main_canvas.yview) scrollbar_y.pack(side='left', fill='y') scrollbar_x = tk.Scrollbar(root, orient='horizontal', command=main_canvas.xview) scrollbar_x.pack(side='bottom', fill='x') main_canvas.configure(yscrollcommand=scrollbar_y.set, xscrollcommand=scrollbar_x.set) # Create a frame to hold all widgets frame_main = tk.Frame(main_canvas) main_canvas.create_window((0, 0), window=frame_main, anchor="nw") # Replace `root` with `frame_main` for all widgets # (e.g., canvas_y_axis1 = FigureCanvasTkAgg(fig_y_axis1, master=frame_main)) root.update() main_canvas.configure(scrollregion=main_canvas.bbox("all")) canvas_y_axis1 = FigureCanvasTkAgg(fig_y_axis1, master=frame_main) GUI import tkinter as tk import pandas as pd import matplotlib.pyplot as plt from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg from tkinter import filedialog root = tk.Tk() root.title("CSV Plotter") root.geometry("1000x800") # Create two canvases for Y-axis 1 and Y-axis 2 fig_y_axis1, ax1 = plt.subplots(nrows=3, ncols=1, figsize=(6, 9)) canvas_y_axis1 = FigureCanvasTkAgg(fig_y_axis1, master=root) canvas_y_axis1.get_tk_widget().place(x=10, y=150, width=500, height=450) fig_y_axis2, ax2 = plt.subplots(nrows=3, ncols=1, figsize=(6, 6)) canvas_y_axis2 = FigureCanvasTkAgg(fig_y_axis2, master=root) canvas_y_axis2.get_tk_widget().place(x=520, y=150, width=500, height=450) def clear_plots(): for ax in ax1: ax.clear() for ax in ax2: ax.clear() fig_y_axis1.tight_layout() fig_y_axis2.tight_layout() canvas_y_axis1.draw() canvas_y_axis2.draw() def load_csv_and_plot(): csv_filename = entry_file.get() start_date = entry_start.get() end_date = entry_end.get() y_axis = entry_y_axis.get() y_axis_additional = entry_y_axis_additional.get() file_path = f'C:/sample5/{csv_filename}' try: data = pd.read_csv(file_path) data['データ日時'] = pd.to_datetime(data['データ日時']) data = data[(data['データ日時'] >= start_date) & (data['データ日時'] <= end_date)] data['weekday'] = data['データ日時'].dt.day_name() data['hour'] = data['データ日時'].dt.hour if y_axis: weekday_mean = data.groupby('weekday')[y_axis].mean() hourly_mean = data.groupby('hour')[y_axis].mean() # Update data count and mean value labels data_count_label.config(text=f"Data count: {len(data)}") weekday_mean_value_label.config(text=f"weekday_mean value ({y_axis}): {weekday_mean}") hourly_mean_value_label.config(text=f"hourly_mean value ({y_axis}): {hourly_mean}") # Clear and redraw the plots for Y-axis 1 ax1[0].clear() ax1[0].plot(data['データ日時'], data[y_axis]) ax1[0].set_xlabel('Date') ax1[0].set_ylabel(y_axis) ax1[0].set_title('Time Series Plot') ax1[1].clear() ax1[1].bar(weekday_mean.index, weekday_mean.values) ax1[1].set_xlabel('Weekday') ax1[1].set_ylabel(f'Average {y_axis}') ax1[1].set_title('Weekday Mean') ax1[2].clear() ax1[2].bar(hourly_mean.index, hourly_mean.values) ax1[2].set_xlabel('Hour') ax1[2].set_ylabel(f'Average {y_axis}') ax1[2].set_title('Hourly Mean') fig_y_axis1.tight_layout() canvas_y_axis1.draw() if y_axis_additional: weekday_mean_additional = data.groupby('weekday')[y_axis_additional].mean() hourly_mean_additional = data.groupby('hour')[y_axis_additional].mean() weekday_mean_additional_value_label.config(text=f"weekday_mean_additional value ({y_axis}): {weekday_mean_additional}") hourly_mean_additional_value_label.config(text=f"hourly_mean_additional value ({y_axis}): {hourly_mean_additional}") # Clear and redraw the plots for Y-axis 2 ax2[0].clear() ax2[0].plot(data['データ日時'], data[y_axis_additional]) ax2[0].set_xlabel('Date') ax2[0].set_ylabel(y_axis_additional) ax2[0].set_title('Time Series Plot') ax2[1].clear() ax2[1].bar(weekday_mean_additional.index, weekday_mean_additional.values) ax2[1].set_xlabel('Weekday') ax2[1].set_ylabel(f'Average {y_axis_additional}') ax2[1].set_title('Weekday Mean (Additional)') ax2[2].clear() ax2[2].bar(hourly_mean_additional.index, hourly_mean_additional.values) ax2[2].set_xlabel('Hour') ax2[2].set_ylabel(f'Average {y_axis_additional}') ax2[2].set_title('Hourly Mean (Additional)') fig_y_axis2.tight_layout() canvas_y_axis2.draw() except FileNotFoundError: error_label.config(text="File not found!") except Exception as e: error_label.config(text=f"Error: {str(e)}") #グラフのクリアボタンの作成 clear_button = tk.Button(root, text="クリア", command=clear_plots) clear_button.place(x=850, y=10, width=150, height=50) #csv名称ボックス label_file = tk.Label(root, text="CSVファイル名 .csvまで入れる") label_file.place(x=50, y=10) entry_file = tk.Entry(root, width=100) entry_file.place(x=10, y=30, width=200, height=30) #期間開始ボックス label_start = tk.Label(root, text="Start Date") label_start.place(x=300, y=10) entry_start = tk.Entry(root, width=100) entry_start.place(x=220, y=30, width=200, height=30) #期間終了ボックス label_end = tk.Label(root, text="End Date") label_end.place(x=500, y=10) entry_end = tk.Entry(root, width=100) entry_end.place(x=420, y=30, width=200, height=30) #Y軸1 ボックス label_y_axis = tk.Label(root, text="Y軸の指定1") label_y_axis.place(x=60, y=80) entry_y_axis = tk.Entry(root, width=100) entry_y_axis.place(x=10, y=100, width=200, height=30) #Y軸2 ボックス label_y_axis_additional = tk.Label(root, text="Y軸2の指定2") label_y_axis_additional.place(x=550, y=80) entry_y_axis_additional = tk.Entry(root, width=100) entry_y_axis_additional.place(x=550, y=100, width=200, height=30) #実行ボタン plot_button = tk.Button(root, text="実行", command=load_csv_and_plot) plot_button.place(x=700, y=10, width=150, height=50) error_label = tk.Label(root, text="") error_label.place(x=750, y=10) # Create labels for data count and mean values data_count_label = tk.Label(root, text="") data_count_label.place(x=50, y=630) weekday_mean_value_label = tk.Label(root, text="") weekday_mean_value_label.place(x=150, y=630) hourly_mean_value_label = tk.Label(root, text="") hourly_mean_value_label.place(x=390, y=630) weekday_mean_additional_value_label = tk.Label(root, text="") weekday_mean_additional_value_label.place(x=580, y=630) hourly_mean_additional_value_label = tk.Label(root, text="") hourly_mean_additional_value_label.place(x=820, y=630) root.mainloop() |
ABOUT ME
スポンサーリンク