diff options
Diffstat (limited to 'diffcsv.py')
| -rwxr-xr-x | diffcsv.py | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/diffcsv.py b/diffcsv.py new file mode 100755 index 0000000..5bfdb05 --- /dev/null +++ b/diffcsv.py @@ -0,0 +1,84 @@ +#!/usr/bin/python + +import streamlit as st +import pandas as pd +import matplotlib.pyplot as plt +import numpy as np + +# æ¥æ¬èªãã©ã³ãã®è¨å® +plt.rcParams['font.family'] = 'Noto Sans CJK JP' + +# Streamlitã¢ããªã±ã¼ã·ã§ã³ã®ã¿ã¤ãã«è¨å® +st.title('CSVãã¡ã¤ã«æ¯è¼ãã£ã¼ã') + +# ã¢ãããã¼ãããã¯ã¹ã®ä½æ +uploaded_files = st.file_uploader("ããã«CSVãã¡ã¤ã«ããã©ãã°ï¼ãããã", type=['csv'], accept_multiple_files=True) + +if uploaded_files: + # ãã¡ã¤ã«ã®æå¤§æ°ã10ã«å¶é + if len(uploaded_files) > 10: + st.error("æå¤§10åã®ãã¡ã¤ã«ãã¢ãããã¼ããã¦ãã ããã") + st.stop() + + # ã¢ãããã¼ãããããã¡ã¤ã«ããªã¹ãã¨ãã¦å¦ç + file_list = [] + for file in uploaded_files: + df = pd.read_csv(file) + file_list.append(df) + st.write(f"ã¢ãããã¼ãããããã¡ã¤ã«ã®ã«ã©ã å: {df.columns.tolist()}") + + date_column = 'DATE' # æ¥ä»ã«ã©ã ã®ããã©ã«ãå¤ + + # åDataFrameã«å¯¾ãã¦æ¥ä»ã«ã©ã ã®åå¨ãç¢ºèª + for df in file_list: + if date_column not in df.columns: # æ¥ä»ã«ã©ã ãåå¨ããªãå ´åãã¨ã©ã¼ã¡ãã»ã¼ã¸ã表示 + st.error(f"åCSVãã¡ã¤ã«ã«ã¯'{date_column}'ã¨ããååã®æ¥ä»ã«ã©ã ãå¿
è¦ã§ããã«ã©ã å: {df.columns.tolist()}") + st.stop() + df[date_column] = pd.to_datetime(df[date_column]) + + # ããããã®ä½æ + fig, ax1 = plt.subplots(figsize=(12, 6)) + ax2 = ax1.twinx() + + for i, df in enumerate(file_list): + # ãã¡ã¤ã«ãã¨ã«ç¸¦è»¸ã®ä½ç½®ã鏿ããããã®ã»ã¬ã¯ãããã¯ã¹ã使 + axis_position = st.selectbox(f'ãã¡ã¤ã«{i+1}ã®ç¸¦è»¸ã®ä½ç½®ã鏿ãã¦ãã ãã', ["å·¦", "å³"], index=0, key=f'axis_position_{i}') + + # ãã¡ã¤ã«ãã¨ã«ãããããããã«ã©ã ã鏿 + col = st.selectbox(f'ãã¡ã¤ã«{i+1}ã®ã«ã©ã ã鏿ãã¦ãã ãã', [col for col in df.columns if col!= date_column], key=f'col_select_{i}') + + # åä¾åãå
¥åããããã®ããã¹ãããã¯ã¹ã使 + legend_name = st.text_input(f'ãã¡ã¤ã«{i+1}ã®å¡ä¾åãå
¥åãã¦ãã ããï¼ããã©ã«ãã¯ã«ã©ã å: {col}ï¼', col, key=f'legend_input_{i}') + + # ãã¼ã¿ã®ã¹ã±ã¼ãªã³ã°ï¼å¿
è¦ã«å¿ãã¦ï¼ + try: + df[col] = pd.to_numeric(df[col], errors='coerce') # æååãæ°å¤ã«å¤æ + min_val = df[col].min() + max_val = df[col].max() + range_val = max_val - min_val + if range_val < 0: + df[col] -= abs(min_val) # ä¸å¿ç·ã0ã«ããããã«ãã¼ã¿ãã¹ã±ã¼ã« + except ValueError: + st.error(f"{col}ã«ã©ã ã®å¤ãæ°å¤ã«å¤æã§ãã¾ããã") + + # 縦軸ã®ä½ç½®ã«å¿ãã¦ãã¼ã¿ãã¬ã¼ã ããããã + if axis_position == "å·¦": + ax1.plot(df[date_column], df[col], label=legend_name, color=f'C{i}') + else: + ax2.plot(df[date_column], df[col], label=legend_name, color=f'C{i}', linestyle='--') + + ax1.set_xlabel(date_column) + ax1.set_ylabel('') + ax2.set_ylabel('') + + # å¡ä¾ã®è¨å®ï¼å·¦è»¸ã®å¡ä¾ãå³å´ã«ãå³è»¸ã®å¡ä¾ãå·¦å´ã«ï¼ + handles1, labels1 = ax1.get_legend_handles_labels() + handles2, labels2 = ax2.get_legend_handles_labels() + ax1.legend(handles2, labels2, loc='upper left') + ax2.legend(handles1, labels1, loc='upper right') + + plt.title('CSV to plot') + st.pyplot(fig) +else: + st.write("CSVãã¡ã¤ã«ãã¢ãããã¼ããã¦ãã ããã") + |
