我的数据如下所示,这是一个多级 pandas 数据框,我正在尝试对其运行一些操作;但需要帮助访问特定级别...
print(df.head())
FB AMZN \
open high low close volume open high low
date
2017-01-03 116.03 117.84 115.51 116.86 20663912 757.92 758.76 747.70
2017-01-04 117.55 119.66 117.29 118.69 19630932 758.39 759.68 754.20
2017-01-05 118.86 120.95 118.32 120.67 19492150 761.55 782.40 760.26
2017-01-06 120.98 123.88 120.03 123.41 28545263 782.36 799.44 778.48
2017-01-09 123.55 125.43 123.04 124.90 22880360 798.00 801.77 791.77
... GOOG \
close volume ... open high low close volume
date ...
2017-01-03 753.67 3521066 ... 778.81 789.63 775.80 786.14 1657268
2017-01-04 757.18 2510526 ... 788.36 791.34 783.16 786.90 1072958
2017-01-05 780.45 5830068 ... 786.08 794.48 785.02 794.02 1335167
2017-01-06 795.99 5986234 ... 795.26 807.90 792.20 806.15 1640170
2017-01-09 796.92 3446109 ... 806.40 809.97 802.83 806.65 1274645
TSLA
open high low close volume
date
2017-01-03 214.86 220.33 210.96 216.99 5923254
2017-01-04 214.75 228.00 214.31 226.99 11213471
2017-01-05 226.42 227.48 221.95 226.75 5911695
2017-01-06 226.93 230.31 225.45 229.01 5527893
2017-01-09 228.97 231.92 228.00 231.28 3979484
[5 rows x 25 columns]
此外,有关级别的更多信息:
df.columns.levels
FrozenList([['FB', 'AMZN', 'NFLX', 'GOOG', 'TSLA'], ['open', 'high', 'low', 'close', 'volume']])
我想遍历这个数据框,一次一个股票代码,并对“关闭”列执行操作。例如,
for stock_ticker in df:
print(stock_ticker) #print stock name
if 'close' <= 100: # if the 'close' column for the specific stock ticker is <= 100
print(stock_ticker is less than or equal to 100)
else:
print(stock_ticker is greater than 100)````
我希望用这个做更复杂的事情;但是,这段代码会让我到达那里。