问题
I'm trying to determine the best way to define a function within a class that reads a specific .csv file if a criterion is met. Then, the function returns the data in the file. But I need it to return the data in a way that allows me to call on those data again in a future function in the class.
Some background: The data I'm reading in are time-stamped temperatures. The columns in each .csv file are 'day', 'hour', 'temp_1', 'temp_2', 'temp_3', 'temp_4'
Then, there are rows of numeric data for each hour of the year, so around 9,000 rows.
I need to define a function that reads in the data from a csv file, then allows me to recall the temp data, and corresponding day and hour in future functions.
### Here I define the class. I just included a shortened version of this.
### The class has more arguments than this. I just included the relevant parameter which is 'group'
class Individual():
def __init__(self,group):
self.group = group
### This is the function, or set of functions, that I'm trying to get running
def return_temp1(self, day, hour):
if self.group =='a':
micro_df = pd.read_csv('ex1.csv')
elif self.group =='b':
micro_df = pd.read_csv('ex2.csv')
return micro_df.temp1,day,hour
def return_temp2(self, day, hour):
if self.group =='a':
micro_df = pd.read_csv('ex1.csv')
elif self.group =='b':
micro_df = pd.read_csv('ex2.csv')
return micro_df.temp2,day,hour
def return_temp3(self, day, hour):
if self.group =='a':
micro_df = pd.read_csv('ex1.csv')
elif self.group =='b':
micro_df = pd.read_csv('ex2.csv')
return micro_df.temp3,day,hour
def return_temp4(self, day, hour):
if self.group =='a':
micro_df = pd.read_csv('ex1.csv')
elif self.group =='b':
micro_df = pd.read_csv('ex2.csv')
return micro_df.temp4,day,hour
### Then, later I need to define more functions where I'm able to call
# on the temperatures pulled from the csv files in the above functions.
# I've included one of those functions below as an example.
def calculate_longwave_radiation(self, temp1):
return 53.1*10**-14*(temp1 +273.15)**6.
I'm fairly new to Python, and very new to using classes. Any help or tips would be greatly appreciated! I know something with the way I've set up the return
lines is causing the issue (or at least part of it)... but I can't figure out how to fix it. Thank you.
回答1:
I'll use a language here which may not be best pythonic language, but as you're new in python, it may guide you in the process.
The pd, or pandas dataframe you are using in your code, are like excel sheets, or a table.
So, what you can do is to create a def called load_data() in your class, and then you load ex1.csv and ex2.csv in df_ex1 and df_ex2 respectively. You can also concatenate the data in a single df if they have the same format. Those dfs should be attributes of the class. Like:
class Individual():
def __init__(self,group):
self.group = group
df_ex1 = pd.DataFrame
Once you do the above you can create all defs you need and reference the dataframes to extract data from them. Like
def return_temp1(self, day, hour):
if self.group =='a':
micro_df = df_ex1[['day', 'hour']].loc[df_ex1['day'] == day])
return micro_df
来源:https://stackoverflow.com/questions/63819209/setting-up-a-function-in-a-class-that-will-read-in-csv-data-in-a-way-that-it-can