pythonpandas

resample multiple columns with pandas


I want to resample daily stock data into monthly stock data.

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)

My 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

Solution

  • You can just apply the resample call to the entire DataFrame:

    data = yf.download(['AAPL', 'TSLA', 'FB'], '2018-01-01', '2019-01-01')['Close']
    
    data_resampled = data.resample('M').last()
    
    print(data)
    

    See also: DataFrame.agg()