0
votes

I'm trying to post data into GA, but I'm getting a indexing error The connection is working as I'm getting response 200, but there seems to be a problem with the for loop which posts all the rows from my dataframe. Anyone who could please help me? thanks!

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)

Error:


IndexingError Traceback (most recent call last) in 4 'pa' : "purchase", 5 'tid' : "xxx", ----> 6 'cid' : df.iloc[i,0], 7 'ti' : df.iloc[i,6], 8 'ec' : "ecommerce",

~\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 # we by definition only have the 0th axis

~\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 except ValueError: 237 raise ValueError(

~\path\lib\site-packages\pandas\core\indexing.py in _validate_key(self, key, axis) 2016 # a tuple should already have been caught by this point 2017 # so don't treat a tuple as a valid indexer -> 2018 raise IndexingError("Too many indexers") 2019 elif is_list_like_indexer(key): 2020 arr = np.array(key)

IndexingError: Too many indexers

1

1 Answers

0
votes

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)