LoginSignup
3
1

More than 3 years have passed since last update.

【完全備忘録】よく使うけど覚えられないコード集

Last updated at Posted at 2020-08-13

はじめに

よく書くけどなぜか覚えられず,毎回ググっているコードをまとめました.

Python

おまじないのあれ

if __name__ == '__main__':
    print('Hello World!')

警告を非表示にする

import warnings
warnings.filterwarnings('ignore')

list を txt ファイルに保存する

# 保存
with open('list.txt', 'w') as f:
    print(*list_obj, sep='\n', file=f)

# 読み込み
with open('list.txt') as f:
    list_obj = f.read().splitlines()
# list_obj = list(map(lambda x: x.split(','), list_obj)) # 二次元以上の場合

リスト内の検索

list = [i for i in list if 'hogehoge' in i]

プログレスバーの表示

from tqdm import tqdm

# jupyter notebook
from tqdm import tqdm_notebook as tqdm

pandas のカラムを全表示

import pandas as pd
pd.set_option('display.max_rows', None)

pandas 内の文字数制限の設定

import pandas as pd
pd.set_option('display.max_colwidth', 1000)

図の大きさの変更

fig = plt.figure(figsize=(20, 10))

Bar Plot

plt.bar(df.index, df['value'], align='center')
plt.xticks(df.index, df.index)

変数の相関関係をヒートマップでプロット

import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns

plt.figure(figsize=(10,10))
sns.heatmap(df, linewidths=0.1, vmax=1.0, square=True, cmap=plt.cm.RdBu, annot=True)

ある文字列より前の文字列を取り出す(ある文字列より後の文字を削除する)

t = 'abc/def'
print(t.split('/')[0]) # abc
print(t.split('/')[1]) # def

# 行列に map するときに指定した文字がないとエラーを吐くため,以下の関数で map すると良い.
def pick_char(t):
    try:
        return t.split('/')[0]
    except IndexError:
        return t
df.map(pick_char)

余計な文字列を削除する

t = '\n\t\r\u3000        abc        \u3000\r\t\n'
print(t.strip()) # abc

pip のキャッシュ削除してインストール

$ pip install --no-cache-dir <Library>

-I で再インストール

極座標変換

np.sin(2*np.pi*data[col]/max_val)
np.cos(2*np.pi*data[col]/max_val)

git

キャッシュの削除

$ git rm -r --cached .

コマンドライン

権限の付与

$ chown -R $USER <dir>

おわりに

便利な文字列操作に出会い次第,追記していく.

3
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1