简体   繁体   中英

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

You can't resample individual columns and assign it to the same DataFrame variable. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM