resample multiple columns with pandas
Asked Answered
S

1

4

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
Shavian answered 11/2, 2019 at 11:1 Comment(0)
B
4

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)
Barbette answered 11/2, 2019 at 11:8 Comment(2)
can you resample only few columns of dataframe?Unquote
It is possible to selectively resample dataframe columns using .agg(): df = df.resample().agg({'col':'method', ...})Inure

© 2022 - 2024 — McMap. All rights reserved.