首页 > 解决方案 > 通过请求将数据发布到 Google Analytics

问题描述

我正在尝试将数据发布到 GA,但出现索引错误当我收到响应 200 时,连接正在工作,但 for 循环似乎存在问题,该循环从我的数据框中发布所有行。谁能帮帮我?谢谢!

endpoint = 'http://www.google-analytics.com/collect'

payload1 = {
                   'v'     : "1",
                   't'     : "event",
                   'pa'    : "purchase",
                   'tid'   : "xxx",
                   'cid'   : df.iloc[i,0],
                   'ti'    : df.iloc[i,6],
                   'ec'    : "ecommerce",
                   'ea'    : "transaction",
                   'ta'    : "aaaa",
                   'tr'    : df.iloc[i,17],
                   'cd1'   : df.iloc[i,0],
                   'cd2'   : df.iloc[i,6],

                   'cu'    : "bbb",
                   "pr1id" : "ccc",
                   'pr1nm' : "ddd",
                   'pr1pr' : df.iloc[i,17],
                   'pr1qt' : 1,
                   'cs'    : "offline"

                      }





for i in df.iterrows():

    r = requests.post(url = endpoint ,

                      data  = payload1,


                      headers={'User-Agent': 'User 1.0'})
    time.sleep(0.1) 
    print(r)

错误:


IndexingError Traceback(最近一次通话最后一次)在 4 'pa':“purchase”,5 'tid':“xxx”,----> 6 'cid':df.iloc[i,0],7 'ti' : df.iloc[i,6], 8 'ec' : "电子商务",

~\path\lib\site-packages\pandas\core\indexing.py in getitem (self, key) 1416 except (KeyError, IndexError, AttributeError): 1417 pass -> 1418 return self._getitem_tuple(key) 1419 else: 1420 # 根据定义,我们只有第 0 个轴

~\path\lib\site-packages\pandas\core\indexing.py in _getitem_tuple(self, tup) 2090 def _getitem_tuple(self, tup): 2091 -> 2092 self._has_valid_tuple(tup) 2093 try: 2094 return self。 _getitem_lowerdim(tup)

~\path\lib\site-packages\pandas\core\indexing.py in _has_valid_tuple(self, key) 233 raise IndexingError("Too many indexers") 234 try: --> 235 self._validate_key(k, i) 236除了ValueError:237引发ValueError(

~\path\lib\site-packages\pandas\core\indexing.py in _validate_key(self, key, axis) 2016 # 到 2017 年此时应该已经捕获了一个元组 # 所以不要将元组视为有效indexer -> 2018 raise IndexingError("Too many indexers") 2019 elif is_list_like_indexer(key): 2020 arr = np.array(key)

IndexingError:索引器过多

标签: pythongoogle-analyticspython-requests

解决方案


df.iterrows() will return the row index and the row itself as a pandas series in each loop. You can then get the row value using the name of the column, if that's easier.

Try instead (I'm guessing the names of the columns):

endpoint = 'http://www.google-analytics.com/collect'
for ind, row in df.iterrows():

    r = requests.post(url = endpoint ,
           data  = {
                   'v'     : "1",
                   't'     : "event",
                   'pa'    : "purchase",
                   'tid'   : "xxx",
                   'cid'   : row["cid"],
                   'ti'    : row["ti"],
                   'ec'    : "ecommerce",
                   'ea'    : "transaction",
                   'ta'    : "aaaa",
                   'tr'    : row["tr"],
                   'cd1'   : row["cd1"],
                   'cd2'   : row["cd2"],
                   'cu'    : "bbb",
                   "pr1id" : "ccc",
                   'pr1nm' : "ddd",
                   'pr1pr' : row['pr1pr'],
                   'pr1qt' : 1,
                   'cs'    : "offline"
                      },
                      headers={'User-Agent': 'User 1.0'
                    }
    )
    time.sleep(0.1) 
    print(r.text)


It's more common to do this sort of operation using the apply function in pandas:

endpoint = 'http://www.google-analytics.com/collect'
def sendRowToGA(row):
    r = requests.post(url = endpoint ,
           data  = {
                   'v'     : "1",
                   't'     : "event",
                   'pa'    : "purchase",
                   'tid'   : "xxx",
                   'cid'   : row["cid"],
                   'ti'    : row["ti"],
                   'ec'    : "ecommerce",
                   'ea'    : "transaction",
                   'ta'    : "aaaa",
                   'tr'    : row["tr"],
                   'cd1'   : row["cd1"],
                   'cd2'   : row["cd2"],
                   'cu'    : "bbb",
                   "pr1id" : "ccc",
                   'pr1nm' : "ddd",
                   'pr1pr' : row['pr1pr'],
                   'pr1qt' : 1,
                   'cs'    : "offline"
                      },
                      headers={'User-Agent': 'User 1.0'
                    }
    )
    print(r.text)


df.apply(sendRowToGA, axis = 1)


推荐阅读