繁体   English   中英

最大和最小熊猫-Python

[英]Max and Min pandas - Python

我有一张表格,查看从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个最大百分比变化值的列
  2. 最低chg百分比值最低的5列
  3. 计算列“ CLOSE”的方差的列

谢谢,

我整理了一个脚本,该脚本似乎可以满足您的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...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM