首页 > 解决方案 > 如何在时间序列中使用 mean() 并将其放到列中?

问题描述

我有这个问题:

bitcoin = pd.read_csv('./Bitcoin/BTC-USD.csv')
bitcoin

然后我每天都有比特币的价值......但实际上我想要 2018 年和 2019 年每个月的平均值:

bitcoin['Date']=pd.to_datetime(bitcoin['Date'],format="%Y-%m-%d")

bitcoin = bitcoin[(bitcoin['Date']>'2018-01-01') & (bitcoin['Date']<'2020-01-01')]

bitcoin_mean =bitcoin['High'].groupby([bitcoin['Date'].dt.year,bitcoin['Date'].dt.month]).mean()

bitcoin_mean

在此处输入图像描述

这很好,但实际上我想要它在 3 个不同的列中,因为我想绘制它,比如 'Year' , 'Day' 和 'Averaged_price' 整数,我该怎么做?

提前致谢!

标签: pythonpandastime-series

解决方案


import pandas as pd

Lownload BTC-USD.csv from here, I also put it on Github Gists.

Read in dataframe:

df = pd.read_csv('BTC-USD.csv')

Then you can get the average for every month:

df.groupby(df['Date'].apply(lambda row: row.split('-')[1])).mean()

which will give you:

enter image description here

(The first column is the months, 01 is January, 02 February, etc...)


Why this works

df['Date'].apply(lambda row: row.split('-')[1]) is a series:

0      08
1      08
2      08
3      08
4      08
       ..
362    08
363    08
364    08
365    08
366    08
Name: Date, Length: 367, dtype: object

It takes the date from each row, ie 2019-08-24, split()s it by -, so it gets a list: ['2019','08','24]. It takes the 1-indexed (ie second) element from that list, ie month. We groupby() this variable & then take the mean() to compute mean of groups (search for mean() here).


推荐阅读