在类中设置一个函数,该函数将以将来在函数中可以引用的方式读取csv数据 [英] Setting up a function in a class that will read in csv data in a way that it can be referenced in future functions

查看:49
本文介绍了在类中设置一个函数,该函数将以将来在函数中可以引用的方式读取csv数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试确定在满足条件的类中定义读取特定.csv文件的函数的最佳方法.然后,该函数返回文件中的数据.但是我需要它以一种允许我在类的未来函数中再次调用这些数据的方式来返回数据.

某些背景:我正在读取的数据是带时间戳的温度.每个.csv文件中的列都是'day', 'hour', 'temp_1', 'temp_2', 'temp_3', 'temp_4'.然后,一年中的每个小时都有数字数据行,因此大约有9,000行.

我需要定义一个函数,该函数从csv文件中读取数据,然后允许我调出临时数据以及将来函数中的相应日期和时间.

### 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.



我是Python的新手,也是使用类的新手.任何帮助或提示将不胜感激!我知道我设置return行的方式会引起问题(或至少是其中一部分)...但是我不知道该如何解决.谢谢.

解决方案

在这里我将使用一种可能不是最好的python语言的语言,但是由于您是python的新手,它可能会在此过程中为您提供指导. /p>

您在代码中使用的pd或pandas数据框,就像excel表格或表格一样.

因此,您可以做的是在您的类中创建一个名为load_data()的def,然后分别在df_ex1和df_ex2中加载ex1.csv和ex2.csv.如果它们具有相同的格式,则也可以将数据串联到一个df中.这些dfs应该是该类的属性.喜欢:

class Individual():
    def __init__(self,group):
        self.group = group
        df_ex1 = pd.DataFrame

完成上述操作后,您可以创建所需的所有def,并引用数据框以从中提取数据.喜欢

    def return_temp1(self, day, hour):
        if self.group =='a':
            micro_df = df_ex1[['day', 'hour']].loc[df_ex1['day'] == day])
        return micro_df

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.

解决方案

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

这篇关于在类中设置一个函数,该函数将以将来在函数中可以引用的方式读取csv数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆