Home > Enterprise >  How to return DataFrame names as strings
How to return DataFrame names as strings

Time:11-25

I have a list of data frames as such:

filters = [U,B,V,R]

Now I perform a check later in my code which checks a given condition and returns the data frames which satisfy this condition. So, for example, I may get returned a new list containing the 'new' data frames as such:

covered_filts = [U,B,V]

Initially, I was plotting the data and assigning the names to the legend of the plot as the DataFrame the data was plotted from by creating a list of the original DataFrame names but as strings such as:

filter_names = ['U', 'B', 'V', 'R']

for s in range(len(covered_filts)): plt.plot(phases, ind_int_flux,label = filter_names[s])

Now this worked perfectly fine if the first DataFrame in the list was covered because the name assignment from my list of strings happened in the same order. However an issue arises when there's an exception that the first DataFrame in the list (U) is not covered but since when assigning labels, the iteration begins from the first string in the list ('U'), it incorrectly assigns that name to the next DataFrame (e.g.B) that is covered.

Therefore, I was wondering if there is another way I can ensure that the filter names are correctly assigned to the DataFrame being plotted.

I am at a bit of a loss as to how to go about doing this and any help would be greatly appreciated.

Thank you

p.s. my idea was such that depending on the data frames returned in my 'covered_filts list, I could create a new list each time to contain the corresponding DataFrame names but I cannot seem to get this to work.

CodePudding user response:

You can use a dict to track your dataframes or use attrs of dataframe:

df_U = pd.DataFrame({'A': [46, 75, 1], 'B': [68, 50, 21], 'C': [98, 23, 48]})
df_B = df_U.copy()
df_V = df_U.copy()

df_U.attrs['name'] = 'U'
df_B.attrs['name'] = 'B'
df_V.attrs['name'] = 'V'

filters = [df_U, df_B, df_V]
covered_filts = [df_U, df_V]

filter_names = [df.attrs.get('name') for df in covered_filts]

Output:

>>> filter_names
['U', 'V']
  • Related