import numpy as np
import pandas as pd
from pandas import Series, DataFramei
import pandas_datareader.data as web
all_data = {ticker: web.get_data_yahoo(ticker)
for ticker in ['AAPL', 'IBM', 'MSFT', 'GOOG']}
price = pd.DataFrame({ticker: data['Adj Close']
for ticker, data in all_data.items()})
volume = pd.DataFrame({ticker: data['Volume']
for ticker, data in all_data.items()})
# 计算价格的百分变化
returns = price.pct_change()
returns.head()
AAPL | IBM | MSFT | GOOG | |
---|---|---|---|---|
Date | ||||
2009-12-31 | NaN | NaN | NaN | NaN |
2010-01-04 | 0.015565 | 0.011841 | 0.015420 | 0.010920 |
2010-01-05 | 0.001729 | -0.012080 | 0.000323 | -0.004404 |
2010-01-06 | -0.015906 | -0.006496 | -0.006137 | -0.025209 |
2010-01-07 | -0.001849 | -0.003461 | -0.010400 | -0.023280 |
# 计算相关系数和协方差
print(returns["MSFT"].corr(returns["IBM"])) # 通过标签的形式
print(returns["MSFT"].cov(returns["IBM"]))
0.49161308372179857
8.80330763108205e-05
returns.MSFT.corr(returns.IBM) # 通过属性的形式
0.49161308372179857
# 协方差和相关系数矩阵
print(returns.corr())
print(returns.cov())
AAPL IBM MSFT GOOG
AAPL 1.000000 0.387306 0.458039 0.463707
IBM 0.387306 1.000000 0.491613 0.407577
MSFT 0.458039 0.491613 1.000000 0.539243
GOOG 0.463707 0.407577 0.539243 1.000000
AAPL IBM MSFT GOOG
AAPL 0.000267 0.000078 0.000108 0.000118
IBM 0.000078 0.000154 0.000088 0.000079
MSFT 0.000108 0.000088 0.000209 0.000121
GOOG 0.000118 0.000079 0.000121 0.000242
# corrwith():计算某列或者行和另一个S或者DF数据之间的相关系数
returns.corrwith(returns.IBM)
AAPL 0.387306
IBM 1.000000
MSFT 0.491613
GOOG 0.407577
dtype: float64
obj = pd.Series(['c', 'a', 'd', 'a', 'a', 'b', 'b', 'c', 'c'])
# 值计数功能
uniques=obj.unique()
uniques
array(['c', 'a', 'd', 'b'], dtype=object)
obj.value_counts() # 默认是降序通过sort=False关闭降序功能pd.value_counts(obj.values, sort=False)
a 3
c 3
b 2
d 1
dtype: int64
# 成员资格
mask = obj.isin(['b', 'c'])
mask
0 True
1 False
2 False
3 False
4 False
5 True
6 True
7 True
8 True
dtype: bool
obj[mask]
0 c
5 b
6 b
7 c
8 c
dtype: object
将pandas.value_counts传给该DataFrame的apply函数
data = pd.DataFrame({'Qu1': [1, 3, 4, 3, 4],
'Qu2': [2, 3, 1, 2, 3],
'Qu3': [1, 5, 2, 4, 4]})
data
Qu1 | Qu2 | Qu3 | |
---|---|---|---|
0 | 1 | 2 | 1 |
1 | 3 | 3 | 5 |
2 | 4 | 1 | 2 |
3 | 3 | 2 | 4 |
4 | 4 | 3 | 4 |
result = data.apply(pd.value_counts).fillna(0)
result
Qu1 | Qu2 | Qu3 | |
---|---|---|---|
1 | 1.0 | 1.0 | 1.0 |
2 | 0.0 | 2.0 | 1.0 |
3 | 2.0 | 2.0 | 0.0 |
4 | 2.0 | 0.0 | 2.0 |
5 | 0.0 | 0.0 | 1.0 |
data.apply(pd.value_counts)
Qu1 | Qu2 | Qu3 | |
---|---|---|---|
1 | 1.0 | 1.0 | 1.0 |
2 | NaN | 2.0 | 1.0 |
3 | 2.0 | 2.0 | NaN |
4 | 2.0 | NaN | 2.0 |
5 | NaN | NaN | 1.0 |
Stay Foolish Stay Hungry