gpt4 book ai didi

python - 是否可以将一个数据帧与另一个数据帧分开?

转载 作者:太空宇宙 更新时间:2023-11-04 09:54:47 25 4
gpt4 key购买 nike

这是我的第一个数据框 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]

是否可以将整个数据帧(其值)与另一个数据帧分开?还是我必须构建某种复杂的循环才能实现这一目标?

最佳答案

这是因为 pandas 将按索引(此处按日期)匹配以应用除法。日期不匹配,因此不会进行任何操作。

你可以试试这个:

A3 = A2.reset_index(drop=True) / (A1.reset_index(drop=True) - 1) * 100

然后您可以重新应用您想要的任何索引:

A3.index = A1.index  # for instance

关于python - 是否可以将一个数据帧与另一个数据帧分开?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46342953/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com