使用下面的代码我正在尝试检查数字列的百分比变化:
import pandas as pd
df = pd.read_csv('./data.txt')
df.pct_change(1)
data.txt:
,AAPL,MSFT,^GSPC
2000-01-03,3.625643,39.33463,1455.219971
2000-01-04,3.319964,38.0059,1399.420044
2000-01-05,3.3685480000000005,38.406628000000005,1402.109985
2000-01-06,3.077039,37.12008,1403.449951
但是上面的代码返回错误:
/opt/conda/lib/python3.5/site-packages/pandas/core/ops.py in na_op(x, y)
1187 if np.prod(xrav.shape) and np.prod(yrav.shape):
1188 with np.errstate(all='ignore'):
-> 1189 result[mask] = op(xrav, yrav)
1190 elif hasattr(x, 'size'):
1191 result = np.empty(x.size, dtype=x.dtype)
TypeError: unsupported operand type(s) for /: 'str' and 'str'
如何使用pct_change方法?移除非数字列(在本例中为日期列),重新运行pct_change,然后重新组合数据列。
DataFrame.pct_change
计算 DataFrame 所有 列 的百分比变化。索引保持不变。 - unutbu