如何解决如何清除数据帧中的所有数据,以便通过for循环进行下一次迭代时它们为空?
我构建了一个脚本,该脚本使用Google Analytics(分析)API来从多个视图ID中获取数据。代码的末尾是一个for循环,它循环运行我构建的许多函数并从中构建数据框。我想发生的是,在每个商店的每个“循环/迭代”之后,数据都将附加到循环外部的新数据帧中。我一直遇到一个问题,尽管数据可以很好地附加到第一个视图ID,但是当它到达第二个视图ID时,它似乎保留了旧数据(从第一次迭代开始),并且在覆盖某些值时将其加倍。
例如此代码。...
lz = zip(LI,DN,VI,V,DA,S,D)
sd = '2020-08-01'
ed = '2020-08-31'
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,parents=[tools.argparser])
flags = parser.parse_args([])
flow = client.flow_from_clientsecrets(
CLIENT_SECRETS_PATH,scope=SCOPES,message=tools.message_if_missing(CLIENT_SECRETS_PATH))
dFI = {'DP7': 'arsseven.dat','DP6': 'arssix.dat','DP4': 'arsfour.dat','DP0': 'arszero.dat'}
hSeg = {'DP7': {'X': 'gaid::3fifjFfj33rokQ','Y': 'gaid::friH58F939Fgoj3I'},'DP6': {'Z': 'gaid::fotkj345DdjgIcg','Y': 'gaid::eofWfjF5834cVfj'},'DP4': {'W': 'gaid::fkormvroE333nr3fg'},'DP0': {'V': 'gaid::Jfoeo455efFggrjor'}}
gaDO = []
gaDT = []
def getGADO(VI,sd,ed):
l = vi_start + str(int(VI))
response = analytics.reports().batchGet(
body={
'reportRequests': [
{
'viewId': l,'dateRanges': [{'sd': sd,'ed': ed}],'metrics': [
{'expression': 'ga:users'},{'expression': 'ga:avgSessionDuration'}
],'dimensions': [
{'name': 'ga:channelGrouping'}
]
}]}).execute()
report_list = response.get('reports')
for report in report_list:
data_rows = report.get('data',{}).get('rows',[])
for row in data_rows:
dimensions_in_row = row.get('dimensions')
metrics_rows = row.get('metrics')
for metrics in metrics_rows:
metrics_values = metrics.get('values')
full_row_data = dimensions_in_row + metrics_values
gaDO.append(full_row_data)
gaDON = []
for i in gaDO:
new_tuple = []
new_tuple.append(i[0])
new_tuple.append(int(i[1]))
new_tuple.append(float(i[2]))
gaDON.append(tuple(new_tuple))
colO = [
#Dimensions
'DCG',#Metrics
'Users','ASD']
dfO = pd.DataFrame(gaDON,columns = colO)
print(dfO)
return dfO
def getGADT(VI,'metrics': [
{'expression': 'ga:users'}
],'dimensions': [
{'name': 'ga:channelGrouping'},{'name': 'ga:segment'}
],'segments': [
{
'segmentId': hSeg[DA][V]
}]
}]}).execute()
report_list = response.get('reports')
for report in report_list:
data_rows = report.get('data',[])
for row in data_rows:
dimensions_in_row = row.get('dimensions')
metrics_rows = row.get('metrics')
for metrics in metrics_rows:
metrics_values = metrics.get('values')
full_row_data = dimensions_in_row + metrics_values
gaDT.append(full_row_data)
gaDTN = []
for i in gaDT:
new_tuple = []
new_tuple.append(i[0])
new_tuple.append(int(float(i[2])))
gaDTN.append(tuple(new_tuple))
colT = [
#Dimensions
'DCG',#Metrics
'TU']
dfT = pd.DataFrame(gaDTN,columns = colT)
print(dfT)
return dfT
dcgX = pd.DataFrame()
for LI,D in locations_zip:
dID_str = str(int(LI))
stor_str = dFI[DA]
storage = file.Storage(stor_str)
credentials = storage.get()
if credentials is None or credentials.invalid:
credentials = tools.run_flow(flow,storage,flags)
http = credentials.authorize(http=httplib2.Http())
analytics = build('analytics','v4',http=http,discoveryServiceUrl=DISCOVERY_URI)
vi_start = 'ga:'
dfOX = getGADO(VI,ed)
dfTX = getGADT(VI,ed)
dcg = pd.merge(dfOX,dfTX,how = 'outer',on = ['DCG']).fillna(0)
dcg = dcg[['DCG','Users','TU','ASD']]
dcg.insert(loc=0,column='LI',value=LI)
dcg.insert(loc=1,column='DN',value=DN)
dcgx = dcgX.append(dcg)
在前两个商店中运行时会产生一些东西...
编辑:更改表以尝试显示并非每个viewID都具有相同的标准化维行/值集,这些维行/值将从viewID到viewID相匹配。例如,CWA仅具有自然搜索和付费搜索,而没有展示。
╔═══════╦═════╦════════════════╦═══════╦═════╦═════╗
║ DI ║ DN ║ DFG ║ Users ║ TU ║ ASD ║
╠═══════╬═════╬════════════════╬═══════╬═════╬═════╣
║ 12345 ║ MHA ║ Organic Search ║ 4392 ║ 589 ║ 30 ║
╠═══════╬═════╬════════════════╬═══════╬═════╬═════╣
║ 12345 ║ MHA ║ Paid Search ║ 3939 ║ 405 ║ 150 ║
╠═══════╬═════╬════════════════╬═══════╬═════╬═════╣
║ 12345 ║ MHA ║ Display ║ 12 ║ 0 ║ 123 ║
╠═══════╬═════╬════════════════╬═══════╬═════╬═════╣
║ 12346 ║ JBA ║ Organic Search ║ 4392 ║ 589 ║ 30 ║
╠═══════╬═════╬════════════════╬═══════╬═════╬═════╣
║ 12346 ║ JBA ║ Organic Search ║ 4392 ║ 96 ║ 30 ║
╠═══════╬═════╬════════════════╬═══════╬═════╬═════╣
║ 12346 ║ JBA ║ Organic Search ║ 2489 ║ 589 ║ 121 ║
╠═══════╬═════╬════════════════╬═══════╬═════╬═════╣
║ 12346 ║ JBA ║ Organic Search ║ 2489 ║ 96 ║ 121 ║
╠═══════╬═════╬════════════════╬═══════╬═════╬═════╣
║ 12346 ║ JBA ║ Paid Search ║ 3939 ║ 405 ║ 150 ║
╠═══════╬═════╬════════════════╬═══════╬═════╬═════╣
║ 12346 ║ JBA ║ Paid Search ║ 3939 ║ 80 ║ 150 ║
╠═══════╬═════╬════════════════╬═══════╬═════╬═════╣
║ 12346 ║ JBA ║ Paid Search ║ 1345 ║ 405 ║ 45 ║
╠═══════╬═════╬════════════════╬═══════╬═════╬═════╣
║ 12346 ║ JBA ║ Paid Search ║ 1345 ║ 80 ║ 45 ║
╠═══════╬═════╬════════════════╬═══════╬═════╬═════╣
║ 12346 ║ JBA ║ Display ║ 12 ║ 0 ║ 123 ║
╠═══════╬═════╬════════════════╬═══════╬═════╬═════╣
║ 12346 ║ JBA ║ Display ║ 12 ║ 1 ║ 123 ║
╠═══════╬═════╬════════════════╬═══════╬═════╬═════╣
║ 12346 ║ JBA ║ Display ║ 400 ║ 0 ║ 60 ║
╠═══════╬═════╬════════════════╬═══════╬═════╬═════╣
║ 12346 ║ JBA ║ Display ║ 400 ║ 1 ║ 60 ║
╠═══════╬═════╬════════════════╬═══════╬═════╬═════╣
║ 12347 ║ CWA ║ Organic Search ║ 4392 ║ 589 ║ 30 ║
╠═══════╬═════╬════════════════╬═══════╬═════╬═════╣
║ 12347 ║ CWA ║ Organic Search ║ 4392 ║ 96 ║ 30 ║
╠═══════╬═════╬════════════════╬═══════╬═════╬═════╣
║ 12347 ║ CWA ║ Organic Search ║ 4392 ║ 12 ║ 30 ║
╠═══════╬═════╬════════════════╬═══════╬═════╬═════╣
║ 12347 ║ CWA ║ Organic Search ║ 2489 ║ 589 ║ 121 ║
╠═══════╬═════╬════════════════╬═══════╬═════╬═════╣
║ 12347 ║ CWA ║ Organic Search ║ 2489 ║ 96 ║ 121 ║
╠═══════╬═════╬════════════════╬═══════╬═════╬═════╣
║ 12347 ║ CWA ║ Organic Search ║ 2489 ║ 12 ║ 121 ║
╠═══════╬═════╬════════════════╬═══════╬═════╬═════╣
║ 12347 ║ CWA ║ Organic Search ║ 5888 ║ 589 ║ 75 ║
╠═══════╬═════╬════════════════╬═══════╬═════╬═════╣
║ 12347 ║ CWA ║ Organic Search ║ 5888 ║ 96 ║ 75 ║
╠═══════╬═════╬════════════════╬═══════╬═════╬═════╣
║ 12347 ║ CWA ║ Organic Search ║ 5888 ║ 12 ║ 75 ║
╠═══════╬═════╬════════════════╬═══════╬═════╬═════╣
║ 12347 ║ CWA ║ Paid Search ║ 3939 ║ 405 ║ 150 ║
╠═══════╬═════╬════════════════╬═══════╬═════╬═════╣
║ 12347 ║ CWA ║ Paid Search ║ 3939 ║ 80 ║ 150 ║
╠═══════╬═════╬════════════════╬═══════╬═════╬═════╣
║ 12347 ║ CWA ║ Paid Search ║ 3939 ║ 600 ║ 150 ║
╠═══════╬═════╬════════════════╬═══════╬═════╬═════╣
║ 12347 ║ CWA ║ Paid Search ║ 1345 ║ 405 ║ 45 ║
╠═══════╬═════╬════════════════╬═══════╬═════╬═════╣
║ 12347 ║ CWA ║ Paid Search ║ 1345 ║ 80 ║ 45 ║
╠═══════╬═════╬════════════════╬═══════╬═════╬═════╣
║ 12347 ║ CWA ║ Paid Search ║ 1345 ║ 600 ║ 45 ║
╠═══════╬═════╬════════════════╬═══════╬═════╬═════╣
║ 12347 ║ CWA ║ Paid Search ║ 7001 ║ 405 ║ 91 ║
╠═══════╬═════╬════════════════╬═══════╬═════╬═════╣
║ 12347 ║ CWA ║ Paid Search ║ 7001 ║ 80 ║ 91 ║
╠═══════╬═════╬════════════════╬═══════╬═════╬═════╣
║ 12347 ║ CWA ║ Paid Search ║ 7001 ║ 600 ║ 91 ║
╠═══════╬═════╬════════════════╬═══════╬═════╬═════╣
║ 12347 ║ CWA ║ Display ║ 12 ║ 0 ║ 123 ║
╠═══════╬═════╬════════════════╬═══════╬═════╬═════╣
║ 12347 ║ CWA ║ Display ║ 12 ║ 1 ║ 123 ║
╠═══════╬═════╬════════════════╬═══════╬═════╬═════╣
║ 12347 ║ CWA ║ Display ║ 400 ║ 0 ║ 60 ║
╠═══════╬═════╬════════════════╬═══════╬═════╬═════╣
║ 12347 ║ CWA ║ Display ║ 400 ║ 1 ║ 60 ║
╚═══════╩═════╩════════════════╩═══════╩═════╩═════╝
我希望它看起来像这样...
╔═══════╦═════╦════════════════╦═══════╦═════╦═════╗
║ DI ║ DN ║ DFG ║ Users ║ TU ║ ASD ║
╠═══════╬═════╬════════════════╬═══════╬═════╬═════╣
║ 12345 ║ MHA ║ Organic Search ║ 4392 ║ 589 ║ 30 ║
╠═══════╬═════╬════════════════╬═══════╬═════╬═════╣
║ 12345 ║ MHA ║ Paid Search ║ 3939 ║ 405 ║ 150 ║
╠═══════╬═════╬════════════════╬═══════╬═════╬═════╣
║ 12345 ║ MHA ║ Display ║ 12 ║ 0 ║ 123 ║
╠═══════╬═════╬════════════════╬═══════╬═════╬═════╣
║ 12346 ║ JBA ║ Organic Search ║ 2489 ║ 96 ║ 121 ║
╠═══════╬═════╬════════════════╬═══════╬═════╬═════╣
║ 12346 ║ JBA ║ Paid Search ║ 1345 ║ 80 ║ 45 ║
╠═══════╬═════╬════════════════╬═══════╬═════╬═════╣
║ 12346 ║ JBA ║ Display ║ 400 ║ 1 ║ 60 ║
╠═══════╬═════╬════════════════╬═══════╬═════╬═════╣
║ 12347 ║ CWA ║ Organic Search ║ 5888 ║ 12 ║ 75 ║
╠═══════╬═════╬════════════════╬═══════╬═════╬═════╣
║ 12347 ║ CWA ║ Paid Search ║ 7001 ║ 600 ║ 91 ║
╚═══════╩═════╩════════════════╩═══════╩═════╩═════╝
我尝试使用...在循环结束时(在dcgx = dcgX.append(dcg)之后)清除dfOX和dfTX数据帧。
del dfOX
del dfTX
但这没用。我也尝试过在它们上使用.iloc [0:0],但这也不起作用。
我不确定如何在for循环的末尾清除数据帧,因此在下一次迭代中它是空的。这是我可以想到的唯一方法,但是还有更好的方法吗?
任何见识将不胜感激!谢谢!
解决方法
尝试将参数drop_duplicates
设置为keep
的{{1}}
假设您的数据帧称为last
我不会讲您的代码,但是请注意,在熊猫中使用循环是一种反模式,除非没有其他选择,否则是一个很大的禁忌。
df
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。