Max和Min熊猫 - Python

时间:2017-07-06 19:29:34

标签: python pandas

我有一张表格,可以查看特定股票从2010年到今天的每日开盘价,最高价,最低价和百分比。

        Open     High      Low    Close  Percentage Chg
Date                                                          
2017-07-03   972.79   974.49   951.00   953.66          -1.481
2017-06-30   980.12   983.47   967.61   968.00          -0.813
2017-06-29   979.00   987.56   965.25   975.93          -1.454

我创建了一个空表。

我想将以下内容添加到空列中:

  1. 具有前5个最大百分比Chg值的列
  2. 具有底部5个最低百分比chg值的列
  3. 计算列“CLOSE”
  4. 的方差的列

    谢谢,

1 个答案:

答案 0 :(得分:0)

我已经整理了一个似乎符合你的3个目标的脚本。

我使用pandas_datareader(版本0.4.0)读取AMAZON股票价格。

以下是代码中的解释:

 import pandas_datareader.data as web
import datetime

start = datetime.datetime(2010, 1, 1)
end = datetime.datetime(2017, 7, 5)

df = web.DataReader("AMZN", 'google', start, end)

#variance calculation
close_var = df['Close'].var()

#make variance a column
df['close_var_col'] = close_var

#selecting the first observation of opeing price from 2010
#base_open = int(df['Open'].head(1))

#creating the percentage difference from previous day
df['perc_chg'] = 100*(df['Close'] - df['Close'].shift(-1))/df['Close'].shift(-1)


#creating a df of top 5 and one of bottom 5 by perc_chg and removing Nan values
df = df.dropna(subset=['perc_chg'])
five_highest_df = df.sort_values(by='perc_chg').tail(5)
five_lowest_df = df.sort_values(by='perc_chg').head(5)

#creating a list of the top 5 (the only way I can think to this is with a string)
list_of_top_5 = str(list(set(list(five_highest_df['perc_chg']))))
list_of_bot_5 = str(list(set(list(five_lowest_df['perc_chg']))))

#Adding the list of the top 5 and bottom 5 to our dataframe
df['list_of_top_5'] = list_of_top_5
df['list_of_bot_5'] = list_of_bot_5

print(df.tail(20))

效率不高,我不确定它作为数据帧特别有用。这是你应该得到的输出。

                   Open     High      Low    Close    Volume  close_var_col
    Date
    2017-06-07  1005.95  1010.25  1002.00  1010.07   2823041   53171.161256
    2017-06-08  1012.06  1013.61  1006.11  1010.27   2767857   53171.161256
    2017-06-09  1012.50  1012.99   927.00   978.31   7647692   53171.161256
    2017-06-12   967.00   975.95   945.00   964.91   9447233   53171.161256
    2017-06-13   977.99   984.50   966.10   980.79   4580011   53171.161256
    2017-06-14   988.59   990.34   966.71   976.47   3974900   53171.161256
    2017-06-15   958.70   965.73   950.86   964.17   5373865   53171.161256
    2017-06-16   996.00   999.75   982.00   987.71  11472662   53171.161256
    2017-06-19  1017.00  1017.00   989.90   995.17   5043408   53171.161256
    2017-06-20   998.00  1004.88   992.02   992.59   4076828   53171.161256
    2017-06-21   998.70  1002.72   992.65  1002.23   2922473   53171.161256
    2017-06-22  1002.23  1006.96   997.20  1001.30   2253433   53171.161256
    2017-06-23  1002.54  1004.62   998.02  1003.74   2879145   53171.161256
    2017-06-26  1008.50  1009.80   992.00   993.98   3386157   53171.161256
    2017-06-27   990.69   998.80   976.00   976.78   3782389   53171.161256
    2017-06-28   978.55   990.68   969.21   990.33   3737567   53171.161256
    2017-06-29   979.00   987.56   965.25   975.93   4302968   53171.161256
    2017-06-30   980.12   983.47   967.61   968.00   3390345   53171.161256
    2017-07-03   972.79   974.49   951.00   953.66   2909108   53171.161256
    2017-07-05   961.53   975.00   955.25   971.40   3652955   53171.161256

 perc_chg                                      list_of_top_5

-0.699951  [12.356073489642865, 9.0991430362990329, 10.96...
-0.019797  [12.356073489642865, 9.0991430362990329, 10.96...
 3.266858  [12.356073489642865, 9.0991430362990329, 10.96...
 1.388731  [12.356073489642865, 9.0991430362990329, 10.96...
-1.619103  [12.356073489642865, 9.0991430362990329, 10.96...
 0.442410  [12.356073489642865, 9.0991430362990329, 10.96...
 1.275709  [12.356073489642865, 9.0991430362990329, 10.96...
-2.383291  [12.356073489642865, 9.0991430362990329, 10.96...
-0.749621  [12.356073489642865, 9.0991430362990329, 10.96...
 0.259926  [12.356073489642865, 9.0991430362990329, 10.96...
-0.961855  [12.356073489642865, 9.0991430362990329, 10.96...
 0.092879  [12.356073489642865, 9.0991430362990329, 10.96...
-0.243091  [12.356073489642865, 9.0991430362990329, 10.96...
 0.981911  [12.356073489642865, 9.0991430362990329, 10.96...
 1.760888  [12.356073489642865, 9.0991430362990329, 10.96...
-1.368231  [12.356073489642865, 9.0991430362990329, 10.96...
 1.475516  [12.356073489642865, 9.0991430362990329, 10.96...
 0.819215  [12.356073489642865, 9.0991430362990329, 10.96...
 1.503681  [12.356073489642865, 9.0991430362990329, 10.96...
-1.826230  [12.356073489642865, 9.0991430362990329, 10.96...