Home > Enterprise >  Cleaner Way to Change Dictionary Structure
Cleaner Way to Change Dictionary Structure

Time:11-13

I have the following object type in python, there are several entries in the data-object just like the one below.

>                                                         G1  \
jobname                                    
x        [3.3935e-06, 6.099100000000001e-06, 8.8048e-06...   
y        [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14...   
yerr     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...   
xfit     [3.3935e-06, 4.0631e-06, 4.7327000000000004e-0...   
yfit     [-0.0215695613, -0.0215695613, -0.0215695613, ...   
xlabel                                      Pulse Time (s)   
ylabel                                          Avg Counts   
params   [-3.6475000000000002e-06, -0.0026722969, -3.20...   
x0                                                  -374.2   
x0_err                                           1.124e 07   

I have been able to change the structure with the following code but I want to be able to do this is a cleaner way. I tried list comprehension, but that doesn't seem to be any cleaner. I also dont want to create all of the empyy lists before initializing the class.

class change_dataframe(): 
    
    def flatten(l):
        return [item for sublist in l for item in sublist]


    def process_data(data, zones:list): 

        for zone in zones: 
            for x in df[zone]['x']:
                zone_list.append(zone)
            xdata_append.append(df[zone]['x'])
            ydata_append.append(df[zone]['y'])
            xfit_append.append(df[zone]['xfit'])
            yfit_append.append(df[zone]['yfit'])

        xdata = flatten(xdata_append)
        ydata = flatten(ydata_append)
        xfit = flatten(xfit_append)
        yfit = flatten(yfit_append)
        
        data = pd.DataFrame({'zones':zone_list,'x':xdata, 'y':ydata})
        fit_data = pd.DataFrame({'xfit':xfit, 'yfit':yfit})

        return fit_data, data
        
                              
# x_data, y_data = process_data(df,zones)
if __name__ == "__main__":
    xdata_append = []
    xfit_append = []
    ydata_append = []
    yfit_append = []
    
    zone_list = []
    zones = ['G1','G2','G3','G4','G5']
    data = change_dataframe.process_data(df,zones)
    print(data)
# zones_list = flatten(zones_append)

Any help would be greatly appreciated.

CodePudding user response:

You wrote

    def process_data(data, zones:list): 

but Author's Intent was apparently

    def process_data(df, zones:list): 

Are we writing class methods, which accept a self parameter, or are we writing top-level functions here?


The flatten helper looks good, though PEP-8 asks you to name the formal parameter lst rather than l.


The repeated applications of flatten seem well suited to a map( ... ) invocation, perhaps with tuple unpack.


The top-level global lists are a bit of a disaster. You have a class already. Banish the globals by making them instance variables, e.g. self.zone_list.

Also, the _append suffix is not great, consider eliding it.

  • Related