我正在尝试使用for循环创建以下数据框。下面是我想用紫色突出显示我想要创建的字段的表格。
以下是我写的代码。当我跑的时候没有任何反应。我正在寻找“收入细分”,“费用细分”和“利润细分”的三个列不会被创建。也没有错误日志。
非常感谢任何帮助!
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)