首页 > 解决方案 > Python:如何将数据框设置为 Python 中函数的参数?

问题描述

我在 CSV 中有 4 列,我想将 CSV 设置为 python 中函数的参数。'key' 应该是我在 CSV 中的第一列。

df = pd.DataFrame({'Country': ['US','France','Germany'],'daycount':['Actual360','Actual365','ActaulFixed'],'frequency':['Annual','Semi','Quart'], 'calendar':['United','FRA','Ger'})

从上面的数据框中,我想将参数设置为以下变量,基于“国家”作为数据框中的键,它应该在以下变量中填充相应的值。我需要一些函数或循环来填充值。这些值将在下一个程序中进一步使用。

day_count = Actual360
comp_frequency = Annual
gl_calendar = UnitedStates

标签: pythonpandas

解决方案


如果我理解正确:

def retrieve_value(attribute, country, df): #input attribute and country as str
    return df.loc[df['Country'] == country, attribute].iloc[0]

前任:

retrieve_value('daycount', 'Germany', df) -> 'ActualFixed'

推荐阅读