Python: Using For Loops to Create New Columns using Conditional If Statements

404
May 14, 2017, at 03:48 AM

I am trying to create the following data frame using for loops. Below is the table I want to output with purple highlighting the fields I want to create.

Below is the code I wrote. When I run it nothing happens. The three columns I'm looking for "Revenue Segment", "Expenses Segment", and "Profit Segment" do not get created. There is no error log either.

Any help is greatly appreciated!

regions = ['East', 'Central', 'West', 'Midwest']
target_var = ['Revenue', 'Expenses', 'Profit']
output_df = pd.DataFrame()
for i in regions:
    for v in target_var: 
        # Create Counter
        temp_df_1 = df_HA_noHA_append_5[df_HA_noHA_append_5['Region'] == i]
        temp_df_1['Counter'] = range(len(temp_df_1))
        temp_df_1['Counter_2'] = temp_df_1['Counter'] + 1
        # Split 
        temp_df_1_AUM_min = temp_df_1[v].min()
        percentile_value = 0.99
        temp_df_1_AUM_max = temp_df_1[v].quantile(percentile_value)
        # Cuts 
        No_Cuts = 3 
        # Intervals 
        Intervals = temp_df_1_AUM_max / No_Cuts

        # Create Interval Table 
        Interval_Min = [temp_df_1_AUM_min, Intervals, Intervals*2]
        Interval_Max = [Intervals, Intervals*2, temp_df_1_AUM_max]
        Segment = ['Low', 'Medium', 'High']    

        # Dataframe
        df_Intervals = pd.DataFrame({'Interval_Min': Interval_Min, 'Interval_Max': Interval_Max, 'Segment': Segment})
        df_Intervals = df_Intervals[['Interval_Min', 'Interval_Max', 'Segment']]

        # Create Segments 
        Segment = []
        # Loop through the AUM 12_MONTH SNAPSHOT column 
        for i in temp_df_1[v]:
            if i >= df_Intervals.iloc[0, 0] and i <= df_Intervals.iloc[0, 1]:
                Segment.append(df_Intervals.iloc[0, 2])
            elif i > df_Intervals.iloc[1, 0] and i <= df_Intervals.iloc[1, 1]:
                Segment.append(df_Intervals.iloc[1, 2])
            elif i > df_Intervals.iloc[2, 0]:
                Segment.append(df_Intervals.iloc[2, 2])
            else:
                Segment.append('')
            # print (df_Intervals)

        # Append list to new column 
        temp_df_1[v] = Segment

        output_df = output_df.append(temp_df_1)
Rent Charter Buses Company
READ ALSO
facial expression classification CNN model in python+ kaggle competition

facial expression classification CNN model in python+ kaggle competition

I really need help in machine learningI wrote facial expression classification CNN model, with 3 layers

418
packet forwarding while ARP poisoning

packet forwarding while ARP poisoning

I wanted to make a "proxy" while ARP poisoning, it works with UDP and if I send a pkt to google I see it on my pc using wireshark

442
How to use variable as a nested dictionary item name?

How to use variable as a nested dictionary item name?

My script gets data from nested dictionary this way:

337
Python_Django - Adding Attending Event option - How to?

Python_Django - Adding Attending Event option - How to?

There are 3 models in my web applicationEvent (self-made), User (imported from django

281