I need help making a function definition where I can call on any dataframe in any folder

Hello,

I need help making a function definition where I can call on any dataframe in any folder. This is what I have so far.

#test function
def get_csv(cat_folder, cat_month):
call_df = pd.read_csv(“data/categories/bymonth/{cat_folder}/{cat_month}”)

And I want this df to be able to show up, but I dont know if this is the way to do it. Im doing it this way because I have a ton of datasets.


This topic was automatically generated from Slack. You can find the original thread here.

Hey @Irwin_Mier_San_Diego_State_University ! Thanks for posting - let me loop @Pranav_Thaenraj_UCSD in on this!

try using the os package in python:
import os
def get_csv(cat_folder, cat_month):
path = os.path.join(cat_folder, cat_month)
call_df = pd.read_csv(path)

Fairly positive that this should work if all of your files are under the same directory, Let me know if it’s not working

Ok, so I should also import os too

do I need to call the entire filepath?

as long as your notebook file is on the same file path as the cat_folder argument you should be able to use this code as is

also yes the os import is required

actually the csv and folders Im referencing is also on data/categories/bymonth which my ipynb file isnt part of

thats why Im calling it starting from that data folder

also when I call this dataset, would I have to do this as an example, like put quotation marks around it?

#test function
def get_csv(data, prv_yr_folder, sp_yr_folder, category_folder, sp_cat_folder, csv_file):
path = os.path.join(data, prv_yr_folder, sp_yr_folder, category_folder, sp_cat_folder, csv_file)
call_df = pd.read_csv(path)
call_df

test_get_csv = get_csv(‘data’, ‘previous_yrs’, ‘2020’, ‘bycategory’, ‘topcat01’, ‘01_2020_core_poi-patterns_Restaurants.csv’)
test_get_csv

also kinda wondering why my df isnt showing up after callinf test_get_csv, is there a way to check to make sure the df is in fact being called?

Hey @Pranav_Thaenraj_UCSD - just flagging these questions from Irwin.

Hi @Niki_Kaz I was able to figure it out. I got whatever I needed to work.

Got it - thanks for the update @Irwin_Mier_San_Diego_State_University ! Apologies for the delay. To prevent any further questions from being overlooked, I’ll go ahead and close this thread out. If you have any more questions or follow-up questions, we’re always here to help! Just be sure to make a new post to help, as we aren’t monitoring old threads at this time. Thanks!

Hi @Niki_Kaz I actually did have one more question

Ill make a new thread