Pandas 通过浮动列表标量排列多列

Pandas 通过浮动列表标量排列多列,pandas,list,math,scalar,Pandas,List,Math,Scalar,假设我有一个如下所示的循环结构: numlist = [0.1, 0.25, 0.5, 0.75, 0.90, 1.1, 1.25, 1.5] numlist = np.array(numlist,dtype=float) apcnt = np.zeros((4,8)) for x in range(5): for y in numlist: apcnt[x][y] = a.iloc[x][0]*numlist[y] print(apcnt) 数据帧“a”如下所示:

假设我有一个如下所示的循环结构:

numlist = [0.1, 0.25, 0.5, 0.75, 0.90, 1.1, 1.25, 1.5]
numlist = np.array(numlist,dtype=float)
apcnt = np.zeros((4,8))
for x in range(5):
   for y in numlist:
     apcnt[x][y] = a.iloc[x][0]*numlist[y]
     print(apcnt)
数据帧“a”如下所示:

           adjusted_power
YearMonth                
Q1           19388.321839
Q2           13435.865517
Q3           12579.123793
Q4           19238.458966 
YearMonth
Q1           19388*0.1   19388*0.25  19388*0.50  19388*0.75  19388*0.9   19388*1.1  19388*1.25  
19388*1.5
Q2           13436(same as above for Q1 example)
Q3           12579(same as above for Q1 example)
Q4           19238(same as above for Q1 example)
我试图得到一个最终的答案,看起来像这样:

           adjusted_power
YearMonth                
Q1           19388.321839
Q2           13435.865517
Q3           12579.123793
Q4           19238.458966 
YearMonth
Q1           19388*0.1   19388*0.25  19388*0.50  19388*0.75  19388*0.9   19388*1.1  19388*1.25  
19388*1.5
Q2           13436(same as above for Q1 example)
Q3           12579(same as above for Q1 example)
Q4           19238(same as above for Q1 example)
我在此表单中遇到索引错误(感谢您的帮助):

文件“”,第7行,在
apcnt[x][y]=a.iloc[x][0]*numlist[y]
索引器:仅整数、片(`:`)、省略号(`…`)、numpy.newaxis(`None`)和整数或
布尔数组是有效的索引

您可以使用广播:

out = pd.DataFrame(a.adjusted_power.values[:,None] * numlist,
                   index=a.index)
输出:

          0        1        2         3        4        5        6        7
--  -------  -------  -------  --------  -------  -------  -------  -------
Q1  1938.83  4847.08  9694.16  14541.2   17449.5  21327.2  24235.4  29082.5
Q2  1343.59  3358.97  6717.93  10076.9   12092.3  14779.5  16794.8  20153.8
Q3  1257.91  3144.78  6289.56   9434.34  11321.2  13837    15723.9  18868.7
Q4  1923.85  4809.61  9619.23  14428.8   17314.6  21162.3  24048.1  28857.7

我们可以做乘法。外部

pd.DataFrame(np.multiply.outer(a['adjusted_power'].values,numlist),index=a.index)
                     0            1  ...             6             7
YearMonth                            ...                            
Q1         1938.832184  4847.080460  ...  24235.402299  29082.482759
Q2         1343.586552  3358.966379  ...  16794.831896  20153.798275
Q3         1257.912379  3144.780948  ...  15723.904741  18868.685690
Q4         1923.845897  4809.614741  ...  24048.073707  28857.688449
[4 rows x 8 columns]

嗨-谢谢,它工作得很好!!“值”后面的[:,None]是什么意思?基本上是
arr=np。数组([1,2,3])[:,None]
为您提供
[[1],[2],[3]]
,然后允许
arr*[4,5]
。更多细节。您还可以执行
np.multiply.outer([1,2,3],[4,5])