问题
I have a google spreadsheet and it has 31 tabs(the 31 days). What I want to do is to use my code to reformat the data (which I have solved), but I can't figure out how to use a for loop to apply the code to all 31 tabs/days. Since each tab is one day of the month, I want the code to go to the first tab, apply the code, and then jump to the next tab and apply the same code. I want this process to go on until it finishes with all 31 tabs.
Below is the code that I have tried, but it doesn't seem to work. I also have tried selecting multiple sheets and trying to select the google sheets' tab as the day, but this doesn't seem to be possible.
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)
One error that I have received is : AttributeError: 'Worksheet' object has no attribute 'worksheet'. I don't know what this means. Overall, I just can't seem to figure out how to apply the codes to all tabs and then to give me a list of all tab names. Additionally, this code doesn't need to be the same code. If someone is able to get this to work, but it rewrites all of the code, I am all for that. The date column should end up as year-month-day hour(military)-minute-second.
来源:https://stackoverflow.com/questions/57400007/how-to-write-a-python-for-loop-to-reformat-31-different-google-sheets-with-panda