yamlでスクリプトの引数やファイルパスなどを読み込む [Python][yaml]

インストール
pip install pyyamlまずは、config.yamlを作成
# config.yamln_class: 10xml_path:/home/ubuntu/disk/xml次にconfi ...
「TXTを1つずつ読み込んで空のDataframeに結合」を繰り返す [Python][Pandas]

from pathlib import Pathdef txt_to_df(str_txt): # TXTをDataframeに変換する、何らかの処理 return dftxt_path = Path("./txt/")all_df = p ...
XML論文からキーワードを抽出し、ワードクラウドで表示[Python]

ElsevierやSpringerなどのオンラインジャーナルは、一般的に、論文がXMLで提供されている。XMLで提供された論文からは、タイトルやアブストラクトや本文が取り出しやすいため、テキスト解析に重宝する。
今回はXM ...
現在時刻を文字列に変換[Python]

from datetime import datetimedatetime.now().strftime("%Y-%m-%d-%H-%M-%S")>>'2020-07-15-05-15-42'
複数のデータを空のdataframeから生成し、結合する方法[Python][Pandas]

以下に3つのdataframe生成し、結合するスクリプトを示す。
pd.concatを使うと、dataframeを縦連結することができる。あまり使わないと思うが、axisのオプションで横連結にすることも可能だ。
setdefaultで辞書に無いキーと値をセットする[Python]

name_list = d = {}for name in name_list: len_name = len(name) head_name = name d.setdefault(name, ()).append((len_name, ...
空のDataFrameに1行ずつデータを追加[pandas]

サンプル
import pandas as pddf = pd.DataFrame()for i in range(5): tmp_series = pd.Series() df = df.append(tmp_series, ...collection.Counterで作った辞書を棒グラフでプロット[python]

import collectionsimport matplotlib.pyplot as pltl = c = collections.Counter(l)plt.bar(list(c.keys()), list(c.values())) ...
TensorDatasetで生画像をTensor化して読み込む[PyTorch]

from glob import globfrom PIL import Imageimport numpy as npimport torchimport torch.utils.data as data_utilsfrom torchv ...
複数のCSVを読み込んで一つのDataframeにまとめる[Python][Pandas]

hogeというフォルダの中に複数のCSVが配置されているとき、
以下のコードで複数のCSVを一つのDataFrame(表)としてまとめることが可能
from glob import globimport pandas ...