python - 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
解决方案
如果我理解正确:
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'
推荐阅读
- xcode - 在苹果 M1(基于新 arm 的 macbook pro)上使用 xcode 12 的 react-native 构建添加 swift 文件失败
- android - 如何使用 ADB 命令触发 WhatsApp 呼叫号码?
- r - 如何返回导致R中错误的索引
- django-models - django更新记录mysql
- javascript - 如何在 JS 中将此 unicode 分数(“¼”和“½”)转换为 Number(1/2, 1/3)?
- redux - Redux-reducer 没有被调用
- java - 为什么在调用子类构造函数时调用超级给出问题?
- android - Android:按下按钮时多次按下按钮
- python-3.x - 安装 Twilio 时遇到问题
- amazon-web-services - Amazon RDS 加密问题