Amit Upadhyay
Amit Upadhyay

Reputation: 59

Declare variable names from an array and then assign each variable as pandas data frame

I have an array of string:

sat_names = ['3254','3269', '3291', '3329', '3352', '3367', 
                  '3414', '3484', '3534', '3565', '3639', '3664']

I want to declare variable bearing the names as follows:

Msat_vir_cut_3254, Msat_vir_cut_3269 ... and so on

I have a function named cut that gives a pandas dataframe as output.

I want to assign the dataframe output of 'cut' function to the variable names using a loop.

## define 'cut' function
def cut(frame,val,f,log):
    if log == True:
        fac = np.log10(f)
        low_lim = np.log10(val) - fac
        up_lim = np.log10(val) + fac
        cut_df = frame.loc[(np.log10(frame) > low_lim) & 
                        (np.log10(frame) < up_lim)]
    else:
        low_lim = val - f
        up_lim = val + f
        cut_df = frame.loc[(frame > low_lim) & (frame < up_lim)]
    return cut_df

Example of cut function usage:

Mh_sat = [3.28634047e+11 3.54157219e+11 3.28634047e+11 3.29435180e+14
 1.46750987e+12 1.65298123e+12 2.34478681e+12 4.23401172e+11
 2.47554291e+11 1.66607462e+11 1.36044543e+12 3.67648925e+12]

Msat_vir_cut_3254 = cut(data['Msat'],Mh_sat[0],3,True)

I want to iterate this process for all variable names as given in sat_names and for all Mh_sat values. sat_names and Mh_sat have same length i.e. 12

Upvotes: 0

Views: 194

Answers (1)

user17144
user17144

Reputation: 438

Assuming cut(..) returns a dataframe,

sat_names = ['3254','3269', '3291', '3329', '3352', '3367', 
              '3414', '3484', '3534', '3565', '3639', '3664']
cut(...).columns = ["Msat_vir_cut_" + i for i in sat_names]

Upvotes: 1

Related Questions