我有一个 5745个数据,并希望将其分为 24 个相等的数据集。我使用了df_split = np.array_split(df,24),它像这样
我需要运行所有数据的函数并返回24输出。每个datframe具有240个数据集(24个数据集)。例如,我希望计算前240个数据的平均值并将其存储为 X 。
我的最终输出应为 X = [3,4,3,5,7,8 ...] 。其中3是前240个数据(df [0])的平均值,而4是后240个数据(df 1)的平均值。连续24套。
答案 0 :(得分:1)
无需拆分DataFrame-因为您必须分别计算每次拆分的平均值。使用"""Requests-OAuthlib sample for Microsoft Graph """
# Copyright (c) Microsoft. All rights reserved. Licensed under the MIT license.
# See LICENSE in the project root for license information.
import os
import uuid
import bottle
import requests_oauthlib
import config
MSGRAPH = requests_oauthlib.OAuth2Session(config.CLIENT_ID,
scope=config.SCOPES,
redirect_uri=config.REDIRECT_URI)
# Enable non-HTTPS redirect URI for development/testing.
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'
# Allow token scope to not match requested scope. (Other auth libraries allow
# this, but Requests-OAuthlib raises exception on scope mismatch by default.)
os.environ['OAUTHLIB_RELAX_TOKEN_SCOPE'] = '1'
os.environ['OAUTHLIB_IGNORE_SCOPE_CHANGE'] = '1'
bottle.TEMPLATE_PATH = ['./static/templates']
@bottle.route('/')
@bottle.view('homepage.html')
def homepage():
"""Render the home page."""
return {'sample': 'Requests-OAuthlib'}
@bottle.route('/login')
def login():
"""Prompt user to authenticate."""
auth_base = config.AUTHORITY_URL + config.AUTH_ENDPOINT
authorization_url, state = MSGRAPH.authorization_url(auth_base)
MSGRAPH.auth_state = state
return bottle.redirect(authorization_url)
@bottle.route('/login/authorized')
def authorized():
"""Handler for the application's Redirect Uri."""
if bottle.request.query.state != MSGRAPH.auth_state:
raise Exception('state returned to redirect URL does not match!')
MSGRAPH.fetch_token(config.AUTHORITY_URL + config.TOKEN_ENDPOINT,
client_secret=config.CLIENT_SECRET,
authorization_response=bottle.request.url)
return bottle.redirect('/graphcall')
@bottle.route('/graphcall')
@bottle.view('graphcall.html')
def graphcall():
"""Confirm user authentication by calling Graph and displaying some data."""
endpoint = config.RESOURCE + config.API_VERSION + '/me'
headers = {'SdkVersion': 'sample-python-requests-0.1.0',
'x-client-SKU': 'sample-python-requests',
'SdkVersion': 'sample-python-requests',
'client-request-id': str(uuid.uuid4()),
'return-client-request-id': 'true'}
graphdata = MSGRAPH.get(endpoint, headers=headers).json()
return {'graphdata': graphdata, 'endpoint': endpoint, 'sample': 'Requests-OAuthlib'}
@bottle.route('/static/<filepath:path>')
def server_static(filepath):
"""Handler for static files, used with the development server."""
root_folder = os.path.abspath(os.path.dirname(__file__))
return bottle.static_file(filepath, root=os.path.join(root_folder, 'static'))
if __name__ == '__main__':
bottle.run(app=bottle.app(), server='wsgiref', host='localhost', port=5000)
,您可以执行以下操作:
groupby
如果您想要单列的均值,则可以这样做:
n = 24
size = np.ceil(len(df) / n).astype(int)
X = df.groupby(df.index % size).mean()
或者,作为列表:
X = df.groupby(df.index % size)['Close'].mean()