Last active
November 28, 2021 17:46
-
-
Save jj-github-jj/89729caf8f88f5b2bc9ccf19849c06b5 to your computer and use it in GitHub Desktop.
acessing dataframe columns using dot notation with namedtuple. Typing var_df2. in jupyter lab will show column names to select as dropdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
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 _ | |
df3.columns=[(re.sub("_+","_",x)) for x in df3.columns.to_list()] #replace 1 or more _ with single _ to compact | |
# + is quantifier for 1 or more https://cheatography.com/davechild/cheat-sheets/regular-expressions/ | |
df3.columns=[x.replace("_","n",1) for x in df3.columns.to_list()] #cant start with underscore so change first underscore | |
return (df3) | |
def abbreviate_names(df): | |
my_ab={'Phase Noise':'PN','Phase_Noise':'PN','Phase_Noise_at':'PN', | |
'Frequency':'Freq','FREQUENCY':'Freq',"Power":"Pwr", | |
'Temperature':'Tmpr', | |
"Carrier":"Carr", | |
"Converter":"Conv", | |
"TEMP_SENSOR":"TS", | |
"Measured":"Meas", | |
"Measurement":"Meas" | |
} | |
for word in my_ab: | |
df2.columns=[x.replace(word,my_ab[word]) for x in df2.columns.to_list()] #list comprehension to substiture abbreviations in names | |
return (df) | |
df2=change_to_valid_column_names(df2) | |
df2=abbreviate_names(df2) | |
var_df2= ntuples(df2) # get a named tuple from columns of dataframe for autofill butfirst convert to valid names |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment