Python
Loops
Files in a directory
import os
directory = os.fsencode(directory_in_str)
for file in os.listdir(directory):
filename = os.fsdecode(file)
if filename.endswith(".txt"):
print("Found a text file"))
continue
else:
continue
Lines in a file
file = open('input.txt','r')
for line in file:
print(line)
Pandas
Conditionally create a column based on another column.
def function(row):
if row['A'] == row['B']:
val = 0
elif row['A'] > row['B']:
val = 1
else:
val = -1
return val
df['C'] = df.apply(f, axis=1)
Fill null values
df = df.fillna('')
Rename columns
df = df.rename(columns={'old_name':'new_name'})
Remove columns
df = df.remove(columns=['column_a', 'column_b'])
Types
Check if string is a valid integer
'14'.isdigit()
Lists
Test is one list is in another list
any(map(lambda v: v in list2, list1))