我有许多股票的以下数据集,并且正在使用以下公式来计算百分比变化df7['Change']=(df7.Close.pct_change())*100
但是,我想如何修改此公式,或编写一个新的公式,以便在股票首次出现在数据框中时将其更改为NaN
。
因此,在下面的示例中,AAPL的更改首次出现在数据中时并未说明-83%
Date Open High Low Close Volume \
2015-11-02 711.059998 721.619995 705.849976 721.109985 1871100
2015-11-03 718.859985 724.650024 714.719971 722.159973 1560800
2015-11-04 722.000000 733.099976 721.900024 728.109985 1704600
2015-11-05 729.469971 739.479980 729.469971 731.250000 1860400
2015-11-06 731.500000 735.409973 727.010010 733.760010 1509600
2015-11-09 730.200012 734.710022 719.429993 724.890015 2065600
2015-11-10 724.400024 730.590027 718.500000 728.320007 1603900
2015-11-02 120.800003 121.360001 119.610001 121.180000 32203300
2015-11-03 120.790001 123.489998 120.699997 122.570000 45519000
2015-11-04 123.129997 123.820000 121.620003 122.000000 44886100
2015-11-05 121.849998 122.690002 120.180000 120.919998 39552700
2015-11-06 121.110001 121.809998 120.620003 121.059998 33042300
2015-11-09 120.959999 121.809998 120.050003 120.570000 33871400
2015-11-10 116.900002 118.070000 116.059998 116.769997 59127900
Adj Close Symbol Change
721.109985 GOOG NaN
722.159973 GOOG 0.145607
728.109985 GOOG 0.823919
731.250000 GOOG 0.431256
733.760010 GOOG 0.343249
724.890015 GOOG -1.208841
728.320007 GOOG 0.473174
120.663499 AAPL -83.361709
122.047573 AAPL 1.147054
121.480003 AAPL -0.465040
120.919998 AAPL -0.885248
121.059998 AAPL 0.115779
120.570000 AAPL -0.404756
116.769997 AAPL -3.151699
我已使用以下代码生成此信息:
import pandas as pd
from pandas.io.data import DataReader
from datetime import datetime
df7 = pd.DataFrame()
symbols = ['GOOG','AAPL']
for symbol in symbols:
ClosingPrice = DataReader(symbol, 'yahoo', datetime(2015,11,1), datetime(2015,11,10))
ClosingPrice = ClosingPrice.reset_index()
ClosingPrice['Symbol'] = symbol
df7 = df7.append(ClosingPrice)
任何帮助将不胜感激。谢谢!
最佳答案
您可以使用groupby方法:
df['Change'] = df.groupby('Symbol').Close.pct_change()
In [20]: df
Out[20]:
Open High Low Close Volume \
Date
2015-11-02 711.059998 721.619995 705.849976 721.109985 1871100
2015-11-03 718.859985 724.650024 714.719971 722.159973 1560800
2015-11-04 722.000000 733.099976 721.900024 728.109985 1704600
2015-11-05 729.469971 739.479980 729.469971 731.250000 1860400
2015-11-06 731.500000 735.409973 727.010010 733.760010 1509600
2015-11-09 730.200012 734.710022 719.429993 724.890015 2065600
2015-11-10 724.400024 730.590027 718.500000 728.320007 1603900
2015-11-02 120.800003 121.360001 119.610001 121.180000 32203300
2015-11-03 120.790001 123.489998 120.699997 122.570000 45519000
2015-11-04 123.129997 123.820000 121.620003 122.000000 44886100
2015-11-05 121.849998 122.690002 120.180000 120.919998 39552700
2015-11-06 121.110001 121.809998 120.620003 121.059998 33042300
2015-11-09 120.959999 121.809998 120.050003 120.570000 33871400
2015-11-10 116.900002 118.070000 116.059998 116.769997 59127900
Adj_Close Symbol Change
Date
2015-11-02 721.109985 GOOG NaN
2015-11-03 722.159973 GOOG 0.001456
2015-11-04 728.109985 GOOG 0.008239
2015-11-05 731.250000 GOOG 0.004313
2015-11-06 733.760010 GOOG 0.003432
2015-11-09 724.890015 GOOG -0.012088
2015-11-10 728.320007 GOOG 0.004732
2015-11-02 120.663499 AAPL NaN
2015-11-03 122.047573 AAPL 0.011471
2015-11-04 121.480003 AAPL -0.004650
2015-11-05 120.919998 AAPL -0.008852
2015-11-06 121.059998 AAPL 0.001158
2015-11-09 120.570000 AAPL -0.004048
2015-11-10 116.769997 AAPL -0.031517
关于Python(Pandas)计算百分比变化,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33947886/