如何编写python for循环以使用pandas重新格式化31个不同的google工作表
我有一个谷歌电子表格,它有31个标签(31天)。我想做的是使用我的代码重新格式化数据(我已经解决了),但我不知道如何使用for循环将代码应用于所有31个选项卡/天。由于每个选项卡都是一个月中的一天,我希望代码转到第一个选项卡,应用代码,然后跳到下一个选项卡并应用相同的代码。我希望这个过程继续下去,直到完成所有31个标签 下面是我尝试过的代码,但似乎不起作用。我也尝试过选择多张工作表,并尝试选择GoogleSheets的标签作为日期,但这似乎是不可能的如何编写python for循环以使用pandas重新格式化31个不同的google工作表,python,pandas,google-sheets,google-colaboratory,Python,Pandas,Google Sheets,Google Colaboratory,我有一个谷歌电子表格,它有31个标签(31天)。我想做的是使用我的代码重新格式化数据(我已经解决了),但我不知道如何使用for循环将代码应用于所有31个选项卡/天。由于每个选项卡都是一个月中的一天,我希望代码转到第一个选项卡,应用代码,然后跳到下一个选项卡并应用相同的代码。我希望这个过程继续下去,直到完成所有31个标签 下面是我尝试过的代码,但似乎不起作用。我也尝试过选择多张工作表,并尝试选择GoogleSheets的标签作为日期,但这似乎是不可能的 Jan = gc.open_by_url('
Jan = gc.open_by_url('with held for privacy reasons')
Jan = Jan.worksheet('01')
#for worksheet in Jan.worksheet:
#while Jan.worksheet is not 31:
if Jan.worksheet != 31:
Jan = get_as_dataframe(Jan)
Jan = pd.DataFrame(Jan)
day_month = Jan.worksheet
new_header = Jan.iloc[0]
Jan = Jan[1:]
Jan.columns = new_header
col_list = ['Time', 'Roof(in)', 'East(in)', 'West(in)', 'North(in)', 'Roof(out)', 'East(out)', 'West(out)', 'North(out)']
Jan = Jan[col_list]
Jan = Jan.dropna(axis=0, how='all')
Jan = Jan[:-2]
Jan.columns = ['DateTime', 'Business_Location_In', 'East_Location_In', 'West_Location_In', 'North_Location_In',
'Business_Location_Out', 'East_Location_Out', 'West_Location_Out', 'North_Location_Out']
Jan['DateTime'] = Jan['DateTime'].str.slice(6)
Jan['DateTime'] = pd.to_datetime('2019-01- ' + worksheet+ Jan['DateTime'])
for filename in Jan:
Jan['Jan'+ day_month] = filenames
while Jan.worksheet() < 31:
Jan = Jan.worksheet(day_month + 1)
elif Jan.worksheet == 31:
Jan = get_as_dataframe(Jan)
Jan = pd.DataFrame(Jan)
day_month = Jan.worksheet
new_header = Jan.iloc[0]
Jan = Jan[1:]
Jan.columns = new_header
col_list = ['Time', 'Roof(in)', 'East(in)', 'West(in)', 'North(in)', 'Roof(out)', 'East(out)', 'West(out)', 'North(out)']
Jan = Jan[col_list]
Jan = Jan.dropna(axis=0, how='all')
Jan = Jan[:-2]
Jan.columns = ['DateTime', 'Business_Location_In', 'East_Location_In', 'West_Location_In', 'North_Location_In',
'Business_Location_Out', 'East_Location_Out', 'West_Location_Out', 'North_Location_Out']
Jan['DateTime'] = Jan['DateTime'].str.slice(6)
Jan['DateTime'] = pd.to_datetime('2019-01- ' + worksheet+ Jan['DateTime'])
for filename in Jan: #this sets the file name to Jan and the day of month
Jan['Jan'+ day_month] = filenames
print(filenames)
Jan=gc.open\u by\u url('出于隐私原因而保留')
一月=一月工作表('01')
#对于1月的工作表,工作表:
#虽然1月工作表不是31号:
如果是一月工作表!=31:
Jan=获取数据帧(Jan)
Jan=pd.数据帧(Jan)
日\月=1月
新的头文件=Jan.iloc[0]
Jan=Jan[1:]
Jan.columns=新标题
col_list=[‘时间’、‘屋顶(内)’、‘东(内)’、‘西(内)’、‘北(内)’、‘屋顶(外)’、‘东(外)’、‘西(外)’、‘北(外)’]
Jan=Jan[col_list]
Jan=Jan.dropna(轴=0,how='all')
一月=一月[:-2]
Jan.columns=['DateTime','Business\u Location\u In','East\u Location\u In','West\u Location\u In','North\u Location\u In',
“商业区外”、“东部区外”、“西部区外”、“北部区外”]
Jan['DateTime']=Jan['DateTime'].str.slice(6)
一月['DateTime']=pd.to_DateTime('2019-01-'+工作表+Jan['DateTime'])
对于1月份的文件名:
Jan['Jan'+日/月]=文件名
而1月工作表()<31:
一月=一月工作表(日/月+1)
elif一月工作表==31:
Jan=获取数据帧(Jan)
Jan=pd.数据帧(Jan)
日\月=1月
新的头文件=Jan.iloc[0]
Jan=Jan[1:]
Jan.columns=新标题
col_list=[‘时间’、‘屋顶(内)’、‘东(内)’、‘西(内)’、‘北(内)’、‘屋顶(外)’、‘东(外)’、‘西(外)’、‘北(外)’]
Jan=Jan[col_list]
Jan=Jan.dropna(轴=0,how='all')
一月=一月[:-2]
Jan.columns=['DateTime','Business\u Location\u In','East\u Location\u In','West\u Location\u In','North\u Location\u In',
“商业区外”、“东部区外”、“西部区外”、“北部区外”]
Jan['DateTime']=Jan['DateTime'].str.slice(6)
一月['DateTime']=pd.to_DateTime('2019-01-'+工作表+Jan['DateTime'])
对于一月中的文件名:#这将文件名设置为一月和月份的日期
Jan['Jan'+日/月]=文件名
打印(文件名)
我收到的一个错误是:AttributeError:“工作表”对象没有属性“工作表”。我不知道这是什么意思。总的来说,我似乎不知道如何将代码应用于所有选项卡,然后给我一个所有选项卡名称的列表。此外,此代码不需要是相同的代码。如果有人能够让它工作,但它重写了所有的代码,我完全支持。日期列应以年-月-日-小时(军事)-分-秒结束