Home > Software design >  Adding Current Date column next to current Data Frame using Python
Adding Current Date column next to current Data Frame using Python

Time:12-14

I'm not fully understanding data frames & am in the process of taking a course on them. This one feels like it should be so easy, but I could really use an explanation.

All I want to do is ADD a column next to my current output that has the CURRENT date in the cells.

I'm getting a timestamp using

time = pd.Timestamp.today()
print (time)

But obviously this is just to print, not connecting it to my other code.

I was able to accomplish this in Google Sheets (once the output lands), but it would be so much cleaner (and informative) if I could do it right from the script.

This is what it currently looks like:

import requests
import pandas as pd
import gspread


gc = gspread.service_account(filename='creds.json')
sh = gc.open_by_key('152qSpr-4nK9V5uHOiYOWTWUx4ojjVNZMdSmFYov-n50')
waveData = sh.get_worksheet(1)

id_list = [
    "/Belmar-Surf-Report/3683/",
    "/Manasquan-Surf-Report/386/",
    "/Ocean-Grove-Surf-Report/7945/",
    "/Asbury-Park-Surf-Report/857/",
    "/Avon-Surf-Report/4050/",
    "/Bay-Head-Surf-Report/4951/",
    "/Belmar-Surf-Report/3683/",
    "/Boardwalk-Surf-Report/9183/",
]

res = []
for x in id_list:
    df = pd.read_html(requests.get("http://magicseaweed.com"  
                      x).text)[0]
    values = [[x], df.columns.values.tolist(), *df.values.tolist()] ## does it go within here?
    res.extend(values)
    res.append([])

waveData.append_rows(res, value_input_option="USER_ENTERED")

I thought it would go within the values, since this is (where I believe) my columns are built? Would love to understand this better if someone is willing to take the time.

CodePudding user response:

In your situation, how about the following modification?

From:

waveData.append_rows(res, value_input_option="USER_ENTERED")

To:

waveData.append_rows(res, value_input_option="USER_ENTERED")

# In this case, please add the following script.
row = len(waveData.get_all_values())
col = max([len(e) for e in res])
time = pd.Timestamp.today()
req = { "requests": [{ "repeatCell": { "cell": { "userEnteredValue": { "stringValue": str(time) } }, "range": { "sheetId": waveData.id, "startRowIndex": row - len(res)   1, "endRowIndex": row, "startColumnIndex": col, "endColumnIndex": col   1 }, "fields": "userEnteredValue" } }] }
sh.batch_update(req)
  • When this script is run, the timestamp of pd.Timestamp.today() is added to the next column of the last column by the batchUpdate method.
  • If you want to add the timestamp to the specific column, please modify range of the above script.
  • Related