Home > Back-end >  How to use Dataframe data in python script?
How to use Dataframe data in python script?

Time:02-10

Hello everyone I'm trying to figure out how to pull data from a data frame using pandas inside my new script?

So I've got excel data saved and I use pandas to pull the data into python but how do I use that data pulled to reference?

I think that is the right verbiage? So below is my code but I want to be able to pull the data from the excel file which has the Case Number, file date, Style. and use that in the "event" portion of my calendar script.

For example :

replace 'description': 'Case 12345',

'start': { 'date': '2022-02-22'

with Case 08A575873 and date 11/17/2022 from the excel sheet.

UPDATE: the event data needs to be JSON string so if I turn the data frame into JSON how would I call this in place?

data = df.to_json(orient='columns')
print(data)

I hope that makes sense? any help would be greatly appreciated!

enter image description here

from google_apis import create_service
from pprint import pprint
import pandas as pd
import openpyxl

df = pd.read_excel (r'C:\Users\phlfo\calendar\Case_Files.xlsx')
print (df)

CLIENT_SECRET_FILE = 'credentials.json'
API_NAME = 'calendar'
API_VERSION = 'v3'
SCOPES = ['https://www.googleapis.com/auth/calendar']



service = create_service(CLIENT_SECRET_FILE, API_NAME, API_VERSION, SCOPES)

calendar_id_jameson = 'primary'

""""
Create an event
"""
event = {
  'summary': 'Test',
  'description': 'Case 12345',
  'start': {
    'date': '2022-02-22',
  },
  'end': {
    'date': '2022-02-22',
  },
  'recurrence': [
    'RRULE:FREQ=DAILY;COUNT=2'
  ],
  'reminders': {
    'useDefault': False,
    'overrides': [
      {'method': 'email', 'minutes': 24 * 60},
      {'method': 'popup', 'minutes': 10},
    ],
  },
}

event = service.events().insert(calendarId='primary', body=event).execute()

print ('Event created: %s' % (event.get('htmlLink')))

here is the print(df) enter image description here

CodePudding user response:

You have a couple different methods from the Pandas data frame to retrieve the information you want. Here you can find a comprehensive guide, but if we use df.iat[x,y] as an example you can do like this:

...
df = pd.read_excel (r'C:\Users\phlfo\calendar\Case_Files.xlsx')
...
event = {
  'summary': 'Test',
  'description': f'{df.iat[5,2]}',
...

I omitted parts of your code for clarity, but notice that you must give a string to the dictionary under 'description': 'this has to be string'. Here I used an f-string, it will replace the result from what is between curly brackets into your string.

  • Related