Skip to content

Instantly share code, notes, and snippets.

@jj-github-jj
jj-github-jj / dict to class
Created November 28, 2021 05:47
turn dict to class for autocomplete features of class
# Turns a dictionary into a class
# type variable name such as t4. and wait for the class attributes to appear in editor using Kite or other language processors
class Dict2Class(object):
def __init__(self, my_dict):
for key in my_dict:
key2=key.replace(" ","_") #key2 for valid names without spaces for object attributes
key2=key2.replace(":","_")
@jj-github-jj
jj-github-jj / dataframe snippets
Last active February 12, 2022 19:26
my dataframe snippets
# create an 8 row by 2 columns dataframe with column labels
df=pd.DataFrame(np.reshape(np.zeros(16),(8,2)),columns=['x','y']) # create an 8 row by 2 columns df
c=df23.columns.to_list()
c[0]
df.iloc[0:,0:2] # rows 0 through end of column 0 and 1
#----------
df.drop(df.columns[[0, 4, 2]], axis = 1, inplace = True) #drop columns using column index
#----------
@jj-github-jj
jj-github-jj / data frame column names auto complete
Last active November 28, 2021 17:46
acessing dataframe columns using dot notation with namedtuple. Typing var_df2. in jupyter lab will show column names to select as dropdown
from collections import namedtuple
import re
#str1, n = re.subn('[0-9]', 'X',str1)
def ntuples(df):
list_of_names = df.columns.values
print(list_of_names[0:2])
list_of_names_dict = {x:x for x in list_of_names}
Varnames = namedtuple('Varnames', list_of_names)