Skip to content

Instantly share code, notes, and snippets.

@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)
@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 / 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 / json to class
Created November 28, 2021 05:49
json to class for autocomplete
# for key,value in x.iteritems():
# new_key = key.replace(" ","_")
# del x[key] # Deleting Previous Key
# x[new_key] = value # Adding Modified key
# for key,value in x.iteritems():
# new_key = key.replace(" ","_")
# del x[key] # Deleting Previous Key
# x[new_key] = value # Adding Modified key
@jj-github-jj
jj-github-jj / tdms snippets
Last active November 28, 2021 05:54
tdms utilities
tdms_file=TdmsFile.read(fpath)
for name, value in tdms_file.properties.items():
print("{0}: {1}".format(name, value))
print ("------ Groups --------")
all_groups = tdms_file.groups()
for group in all_groups:
print ("Group Name:",group.name)
@jj-github-jj
jj-github-jj / plotly snippets.py
Last active November 9, 2022 03:59
plotly snippets
# speed up figures with lots of data
pip install plotly-resampler
from plotly_resampler import register_plotly_resampler, unregister_plotly_resampler
register_plotly_resampler(mode="auto", default_n_shown_samples=1000)
import plotly.io as pio
pio.renderers.default = "notebook" # patch for exporting to html in vscode for plotly interactive plots
@jj-github-jj
jj-github-jj / numpy utility snippets.py
Last active December 28, 2021 14:13
numpy utility snippets
def find_nearest(array, value):
array = np.asarray(array)
idx = (np.abs(array - value)).argmin()
return idx,array[idx] #return in x,y format x is index, y is value
[print (i,v/1e9) for i,v in enumerate(x)] #print index and value of elements in list
def avg_dB(dB):
@jj-github-jj
jj-github-jj / dataframeto numbers only rows.py
Created November 28, 2021 06:01
# convert data frame strings to numeric where possible and drop rows with non numeric content
# convert data frame strings to numeric where possible and drop rows with non numeric content
df23=df23.apply(pd.to_numeric, errors='ignore') # converts to numbers when possible for all columns
df23
c=df23.columns.to_list()
data_columns=[c[x] for x in range(1,6)]
# Eliminate invalid data from dataframe (see Example below for more context)
num_df = (df23.drop(data_columns, axis=1)
.join(df23[data_columns].apply(pd.to_numeric, errors='coerce'))) #numeric dataframe
@jj-github-jj
jj-github-jj / tdms group channel names autocomplete.py
Last active November 29, 2021 03:28
tdms group names channels name autocomplete. convert group names and convert channel names function do the trick of returning namedtuple that can be used for autocomplete
def ntuples_list(list_of_names):
"""
"""
list_of_names_dict = {x:x for x in list_of_names}
Varnames = namedtuple('Varnames', list_of_names)
return Varnames(**list_of_names_dict)
def change_to_valid_column_names(df3):
df3.columns=[(re.sub("[/' ()-+]","_",x)) for x in df3.columns.to_list()] #replace invalid char with _
@jj-github-jj
jj-github-jj / labview xy plot.py
Created December 6, 2021 05:52
tuple of tuples labview xy multiplot data to py class
from dataclasses import dataclass, is_dataclass
def nested_dataclass(*args, **kwargs):
def wrapper(cls):
cls = dataclass(cls, **kwargs)
original_init = cls.__init__
def __init__(self, *args, **kwargs):
for name, value in kwargs.items():
field_type = cls.__annotations__.get(name, None)
if is_dataclass(field_type) and isinstance(value, dict):