gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 19:54:17 26 4
gpt4 key购买 nike

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com