Created
June 4, 2023 08:03
-
-
Save daitomanabe/5d119786b7d4e293fe8043b155e55f6f to your computer and use it in GitHub Desktop.
combine and vis csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import argparse | |
import pandas as pd | |
import matplotlib.pyplot as plt | |
from datetime import datetime | |
# コマンドライン引数のパーサーを作成 | |
parser = argparse.ArgumentParser(description='CSVファイルの結合') | |
parser.add_argument('input_dir', type=str, help='入力ディレクトリのパス') | |
parser.add_argument('output_file', type=str, help='出力ファイルの名前') | |
args = parser.parse_args() | |
# 入力ディレクトリと出力ファイル名を取得 | |
input_dir = args.input_dir | |
output_file = args.output_file | |
# フォルダ内のすべてのCSVファイルを取得 | |
csv_files = [file for file in os.listdir(input_dir) if file.endswith(".csv")] | |
# フォルダ内のすべてのCSVファイルを取得し、ファイル名順にソート | |
csv_files = sorted([file for file in os.listdir(input_dir) if file.endswith(".csv")]) | |
# 列ごとのデータを保持するリスト | |
columns_data = [] | |
# CSVファイルを読み込んで列ごとのデータをリストに追加 | |
for file in csv_files: | |
file_path = os.path.join(input_dir, file) | |
df = pd.read_csv(file_path) | |
columns_data.append(df) | |
# 列ごとのデータを結合してデータフレームを作成 | |
combined_df = pd.concat(columns_data, axis=1) | |
# タイムスタンプを取得し、ファイル名に追加 | |
timestamp = datetime.now().strftime("%Y%m%d%H%M%S") | |
output_filename = f"{output_file}_{timestamp}.csv" | |
# 出力ファイルのパスを作成 | |
output_path = os.path.join(input_dir, output_filename) | |
# 結合したデータフレームをCSVファイルとして保存 | |
combined_df.to_csv(output_path, index=False) | |
# 全ての列を可視化 | |
for column in combined_df.columns: | |
plt.plot(combined_df[column], label=column) | |
plt.xlabel("x") | |
plt.ylabel("y") | |
plt.title("vis") | |
plt.legend() | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment