这是我的第一个数据框的头部,A1:
K37L K37M K37N K37P K37Q K37R K37S K37T K37U K37V ... \
1997-01-01 79.8 80.4 72.8 36.7 0.0 90.0 96.9 92.2 79.8 93.7 ...
1997-02-01 79.1 81.7 73.6 36.7 0.0 90.2 97.4 92.2 80.3 93.5 ...
1997-03-01 79.2 80.8 73.2 37.0 0.0 90.3 97.5 92.0 80.4 93.2 ...
K385 K386 K387 K388 K389 K38A K38B K38C K38D K38E
1997-01-01 67.4 79.1 227.0 83.8 82.1 94.0 74.2 88.4 81.1 73.9
1997-02-01 67.5 79.2 220.9 83.9 82.1 94.2 74.5 88.5 81.1 74.3
1997-03-01 67.5 79.3 218.9 84.0 82.2 94.3 74.7 88.7 81.1 74.4
[3 rows x 27 columns]
这是我的第二个数据框的头部,A2:
K37L K37M K37N K37P K37Q K37R K37S K37T K37U K37V ... \
1996-01-01 78.9 79.4 71.7 36.7 0.0 88.7 94.1 90.7 80.2 98.9 ...
1996-02-01 79.3 81.0 72.7 36.7 0.0 88.7 94.3 90.9 79.8 98.7 ...
1996-03-01 79.8 80.4 72.7 36.7 0.0 89.0 94.6 91.0 79.6 98.6 ...
K385 K386 K387 K388 K389 K38A K38B K38C K38D K38E
1996-01-01 70.9 78.7 257.8 83.9 79.7 92.2 73.8 86.4 79.6 74.0
1996-02-01 70.7 78.7 257.2 83.9 79.8 92.6 73.7 86.6 79.9 73.9
1996-03-01 70.9 78.7 257.3 83.9 80.1 92.6 73.8 87.2 80.1 74.0
[3 rows x 27 columns]
我想做的是:A2 /(A1 - 1)* 100
最重要的部分是我想将A1中的所有值除以A2。
我的尝试:
A3 = A2 / (A1-1)*100
print(A3.head(3))
收率:
K37L K37M K37N K37P K37Q K37R K37S K37T K37U K37V ... \
1996-01-01 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ...
1996-02-01 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ...
1996-03-01 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ...
K385 K386 K387 K388 K389 K38A K38B K38C K38D K38E
1996-01-01 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
1996-02-01 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
1996-03-01 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
[3 rows x 27 columns]
是否可以将整个数据帧(其值)与另一个数据帧分开?或者我将不得不构建某种复杂的循环来实现这一目标?
答案 0 :(得分:2)
这是因为pandas将按索引(此处按日期)匹配以应用除法。日期不匹配,因此不会进行任何操作。
你可以试试这个:
A3.index = A1.index # for instance
然后,您可以重新应用您希望的任何索引:
CREATE OR REPLACE FUNCTION public.get_no_urut_pp (OUT urut1 character varying)
RETURNS character varying AS
$BODY$
BEGIN
insert into no_urut_pp values (28);
select max(nomor) into urut1 from no_urut_pp;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
答案 1 :(得分:2)
索引不匹配,因此请NaN
s
一种可能的解决方案是除以numpy数组,如果两个df具有相同的大小:
A3 = A2 / (A1.values-1)*100
print(A3.head(3))
K386 K387 K388 K389 K38A \
1996-01-01 100.768246 114.070796 101.328502 98.273736 99.139785
1996-02-01 100.639386 116.962256 101.206273 98.397041 99.356223
1996-03-01 100.510856 118.081689 101.084337 98.645320 99.249732
K38B K38C K38D K38E
1996-01-01 100.819672 98.855835 99.375780 101.508916
1996-02-01 100.272109 98.971429 99.750312 100.818554
1996-03-01 100.135685 99.429875 100.000000 100.817439