用熊猫重新采样多列

时间:2019-02-11 11:01:57

标签: python pandas

我想将每日库存数据重新采样为每月库存数据。

data = yf.download(['AAPL', 'TSLA', 'FB'], '2018-01-01', '2019-01-01')['Close']

for column in data:
    data[column].resample('M').last()
    print(data[column])

print(data)

我的数据:

                  AAPL          FB        TSLA
Date                                          
2018-01-02  172.259995  181.419998  320.529999
2018-01-03  172.229996  184.669998  317.250000
2018-01-04  173.029999  184.330002  314.619995
2018-01-05  175.000000  186.850006  316.579987
2018-01-08  174.350006  188.279999  336.410001

1 个答案:

答案 0 :(得分:1)

您不能对单个列进行重新采样并将其分配给相同的DataFrame变量。您可以将重采样调用应用于整个DataFrame:

data = yf.download(['AAPL', 'TSLA', 'FB'], '2018-01-01', '2019-01-01')['Close']

data_resampled = data.resample('M').last()

print(data)