[Complete memorandum] A collection of codes that I often use but cannot remember

Introduction

I write it a lot, but for some reason I can't remember it, so I've put together the code that I google every time.

Python

That's magic

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

Hide warnings

import warnings
warnings.filterwarnings('ignore')

Save the list to a txt file

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

#Read
with open('list.txt') as f:
    list_obj = f.read().splitlines()
# list_obj = list(map(lambda x: x.split(','), list_obj)) #For two or more dimensions

Search in list

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

Progress bar display

from tqdm import tqdm

# jupyter notebook
from tqdm import tqdm_notebook as tqdm

Show all pandas columns

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

Setting the character limit in pandas

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

Change the size of the figure

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

Bar Plot

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

Extract the character string before a certain character string (delete the character after a certain character string)

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

#If there is no specified character when mapping to the matrix, an error will be thrown, so it is better to map with the following function.
def pick_char(t):
    try:
        return t.split('/')[0]
    except IndexError:
        return t
df.map(pick_char)

Remove extra strings

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

Delete pip cache and install

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

Reinstall with -I

git

Delete cache

$ git rm -r --cached .

Command line

Granting authority

$ chown -R $USER <dir>

in conclusion

I will add it as soon as I encounter a convenient character string operation.

Recommended Posts

[Complete memorandum] A collection of codes that I often use but cannot remember
A memorandum that you will often use in Python's Selenium
I know, but I can't stop--Python is a collection of common mistakes
A memorandum until using mecab on a machine that cannot use sudo
A memorandum of how to write pandas that I tend to forget personally
Features of regular expression modules that I often use personally in Python
A collection of Numpy, Pandas Tips that are often used in the field
A template that I often use when making Discord BOT in Python (memorial note)
String manipulation with python & pandas that I often use
A collection of code often used in personal Python
Super simple: A collection of shells that output dates
A collection of Excel operations often used in Python
python Condition extraction from a list that I often forget
[Django] A collection of scripts that are convenient for development