为了创建规范化数组,您可以使用数组和循环。要按单个列表中的值对所有列表进行排序,您可以将它们合并到一个数据框中,然后对数据框进行排序。
试试这个代码:
import numpy as np
import pandas as pd
avg_price = [10, 3, 5, 5, 8]
avg_costs = [8, 3, 5, 9, 4]
std_dev_price = [1, 2, 1, 1, 2]
std_dev_costs = [1, 1, 2, 2, 1]
print('Start:')
print(' avg_price',avg_price,'\n','avg_costs',avg_costs,'\n','std_dev_price',std_dev_price,'\n','std_dev_costs',std_dev_costs,'\n')
num_reps = 10
price = []
costs = []
for i in range(len(avg_price)):
price.append(np.random.normal(avg_price[i], std_dev_price[i], num_reps))
costs.append(np.random.normal(avg_costs[i], std_dev_costs[i], num_reps))
# merge lists to dataframe
dd = {'avg_price':avg_price,'avg_costs':avg_costs,'std_dev_price':std_dev_price,'std_dev_costs':std_dev_costs,'price':price,'costs':costs}
df = pd.DataFrame(dd)
# sort all columns by avg_costs
df = df.sort_values(by=['avg_costs'], ascending=False)
# revert back to lists
avg_price = df['avg_price'].to_list()
avg_costs = df['avg_costs'].to_list()
std_dev_price = df['std_dev_price'].to_list()
std_dev_costs = df['std_dev_costs'].to_list()
price = df['price'].to_list()
costs = df['costs'].to_list()
print('Sorted by avg costs:')
print(' avg_price',avg_price,'\n','avg_costs',avg_costs,'\n','std_dev_price',std_dev_price,'\n','std_dev_costs',std_dev_costs,'\n')
print('Full Dataframe:')
print(df)
输出(对齐)
Start:
avg_price [10, 3, 5, 5, 8]
avg_costs [8, 3, 5, 9, 4]
std_dev_price [1, 2, 1, 1, 2]
std_dev_costs [1, 1, 2, 2, 1]
Sorted by avg costs:
avg_price [5, 10, 5, 8, 3]
avg_costs [9, 8, 5, 4, 3]
std_dev_price [1, 1, 1, 2, 2]
std_dev_costs [2, 1, 2, 1, 1]
Full Datframe:
avg_price avg_costs std_dev_price std_dev_costs price costs
3 5 9 1 2 [2.5092028090378355, 4.81567722966441, 5.49058... [10.563731356333232, 6.647727831982642, 8.0096...
0 10 8 1 1 [9.341336323005978, 9.810670988947805, 10.7077... [7.339563107482252, 8.790936460961769, 8.40626...
2 5 5 1 2 [5.656265155737828, 4.975811559532342, 6.49261... [1.6639841191325848, 3.691022076817463, 7.0424...
4 8 4 2 1 [7.868428684276111, 8.65633376629758, 11.67775... [4.141047169505752, 4.25009712746727, 2.993419...
1 3 3 2 1 [2.920446641165024, 3.958967186635695, 3.31161... [3.5024403403983557, 3.7703884020352803, 2.708...