python - 如何使用函数中的参数来定义要在函数中使用的数据框的名称?

标签 python pandas function arguments

I need to repeat similar operations in data frames with identycal structures but refering to different years; The objective is to generate a function (the one included is just for the sake of the presentation of the problem) that calls the data using one of its arguments (the year in this case). I would like to be able to select the data frame within the function using its name, in this case using the last part of its name (its year) as an argument of the function'

    import pandas as pd
    import numpy as np

Suppose you have three data frames, one for each year

    df_2005 = pd.DataFrame(np.random.randint(0,10,size=(10, 4)), columns=list('ABCD'))
    df_2006 = pd.DataFrame(np.random.randint(0,10,size=(10, 4)), columns=list('ABCD'))
    df_2007 = pd.DataFrame(np.random.randint(0,10,size=(10, 4)), columns=list('ABCD'))

This functions extracts, as an example, some data from the data frame to generate a different variable

    def func_1 (Year):
       data=data_+year
       X=data.iloc[[1,2],[2,3]].copy() 
       return X

This is the way I intend to use the function:

 subdata_2005=func_1('2005')

I have tried several things like data_+year, data_`year', but nothing seems to work. I was not able to find answers to similar questions that could help me in this case. Any suggestion would be highly appreciated

最佳答案

我建议的是像字典/数据帧列表,例如

而不是尝试这个行不通的方法

for i in range(2005,2007):
    'df_'+i = pd.DataFrame(np.random.randint(0,10,size=(10, 4)), columns=list('ABCD'))

尝试

dfs = {}
for i in range(2005,2007):
    dfs[i] = pd.DataFrame(np.random.randint(0,10,size=(10, 4)), columns=list('ABCD'))

因此

def func_1 (Year):
       data=data_+year
       X=data.iloc[[1,2],[2,3]].copy() 
       return X

因此将是

def func_1 (Year):
       data = dfs[Year]
       X=data.iloc[[1,2],[2,3]].copy() 
       return X

关于python - 如何使用函数中的参数来定义要在函数中使用的数据框的名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59632254/

相关文章:

python - Python 中未发现的单元测试 - Visual Studio Code

python - BeautifulSoup 断言错误

python - 在 pandas Dataframe 的每一行中搜索字符串并返回找到的列名称

python - 在 Pandas 中添加数据框

Matlab:如何合法关闭程序?

python - Pandas GroupBy,将新的数字列表列与另一列数字列表进行比较

Python 字符串文字连接

python - 使用tornado.auth.TwitterMixin 和回调URL?

javascript - 使用函数声明时的自定义函数(也称为惰性函数定义)

javascript - 如何自动检测数组中的值并从另一个数组设置背景颜色