使用Pandas的替代Google Analytics IO

时间:2016-05-19 08:37:43

标签: pandas google-analytics

pandas 0.17.1版本有depreciated pandas.io.ga模块。

现在使用谷歌分析与熊猫有什么替代方案?是否有可靠的库现在可以使用?

2 个答案:

答案 0 :(得分:7)

创建Google2Pandas module是为了解决这个特定问题。没有什么花哨的,只是按照它在盒子上说的那样。

V3:

from google2pandas import GoogleAnalyticsQuery

query = {\
    'ids'           : <valid_ids>,
    'metrics'       : 'pageviews',
    'dimensions'    : ['date', 'pagePath', 'browser'],
    'filters'       : ['pagePath=~iPhone', 'and', 'browser=~Firefox'],
    'start_date'    : '8daysAgo',
    'max_results'   : 10}

conn = GoogleAnalyticsQuery(secrets='client_secrets_v3.json',
                            token_file_name='analytics.dat')
df, metadata = conn.execute_query(**query)

V4:

from google2pandas import GoogleAnalyticsQueryV4

query = {
    'reportRequests': [{
        'viewId' : <valid_ids>,

        'dateRanges': [{
            'startDate' : '8daysAgo',
            'endDate'   : 'today'}],

        'dimensions' : [
            {'name' : 'ga:date'}, 
            {'name' : 'ga:pagePath'},
            {'name' : 'ga:browser'}],

        'metrics'   : [
            {'expression' : 'ga:pageviews'}],

        'dimensionFilterClauses' : [{
            'operator' : 'AND',
            'filters'  : [
                {'dimensionName' : 'ga:browser',
                 'operator' : 'REGEXP',
                 'expressions' : ['Firefox']},

                {'dimensionName' : 'ga:pagePath',
                 'operator' : 'REGEXP',
                 'expressions' : ['iPhone']}]
        }]
    }]
}


conn = GoogleAnalyticsQueryV4(secrets='client_secrets_v4.json')
df = conn.execute_query(query)

答案 1 :(得分:2)

Remote Data Access

您应该替换以下内容的导入:

from pandas.io import data, wb

     With:

from pandas_datareader import data, wb

pandas.io.data和pandas.io.ga中的函数将来自各种Internet源的数据提取到DataFrame中。目前支持以下来源:

雅虎!金融 Google财经 St.Louis FED(FRED) Kenneth French的数据库 世界银行 Google Analytics

https://github.com/pydata/pandas-datareader

是GA在pandas_datareader中进行的讨论,但到目前为止还没有(未经过测试,问题在于:https://github.com/pandas-dev/pandas/issues/8961),到目前为止,此问题已通过“googleanalytics”软件包解决。

示例:

import googleanalytics as ga
accounts = ga.authenticate()
profile = accounts[0].webproperties[0].profile
pageviews = profile.core.query.metrics('pageviews').range('yesterday').value
print(pageviews)

https://github.com/debrouwere/google-analytics

example for the pandas-datareader

工作代码:

import pandas_datareader.data as web
import datetime

start = datetime.datetime(2010, 1, 1)
end = datetime.datetime(2017, 11, 24)
f = web.DataReader("F", 'google', start, end)
f.loc['2017-11-24']

result with a day Sample of how look the dataframe

希望它有所帮助!