-
-
Save AkiyonKS/39413fb121ad96d213a800b48151a4f9 to your computer and use it in GitHub Desktop.
add labels to trains
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 pandas as pd | |
import os | |
import shutil | |
# trains.csv読み込み | |
filename = "trains" | |
file_path = "../csv/" + filename + ".csv" | |
df = pd.read_csv(file_path) | |
# photo_idをintに変換 | |
df['photo_id'] = list(map(int, df['photo_id'])) | |
# label列に"形式_鉄道会社名"を代入 | |
df["label"] = list(df.apply(lambda x: "_".join([str(x["形式"]), str(x.company_name)]), axis=1)) | |
# reindexで列の並べ替え | |
df = df.reindex(columns=[ | |
"photo_id", | |
"label", | |
"形式", | |
"company_name", | |
"愛称", | |
"編成", | |
"路線", | |
"車両", | |
"alt", | |
"アクセス数", | |
"投稿日時", | |
"撮影場所", | |
"撮影日時", | |
"src", | |
"company_name0", | |
"country" | |
]) | |
# 並べ替え | |
sort_arr = ['label','形式','company_name','編成','車両','愛称','路線','photo_id'] | |
df = df.sort_values(sort_arr).reset_index().drop(columns="index") | |
# photo_idを文字列に変換 | |
df["photo_id"] = list(map(lambda x: str(int(x)), df["photo_id"])) | |
# バックアップ取っておく | |
if os.path.isfile(file_path): | |
shutil.copy2(file_path, '../csv/' + filename + '_backup.csv') | |
# ファイルに保存 | |
df.to_csv(file_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment