[英]How can I use a variable value to call a dataframe?
Say that I'm given a dataframe that summarizes different companies: 假设我有一个汇总不同公司的数据框:
summary=pandas.DataFrame(columns=['Company Name', 'Formation Date', 'Revenue', 'Profit', 'Loss'])
And then say each company in that dataframe has its own corresponding dataframe, named after the company, giving a more in-depth picture of the company's history and stats. 然后说该数据框中的每个公司都有自己的对应数据框,以公司名称命名,从而更深入地了解了公司的历史和统计数据。 Something like: 就像是:
exampleco=pandas.Dataframe(columns=['Date', 'Daily Profit', 'Daily Loss', 'Daily Revenue'])
I have a script that processes each row of the summary
dataframe, but I would like to grab the name from row['Company Name']
and use it to access the company's own dataframe. 我有一个脚本来处理summary
数据框的每一行,但是我想从row['Company Name']
中获取row['Company Name']
并使用它来访问公司自己的数据框。
In other words I'd love it if there was something that worked like this: 换句话说,如果有这样的事情,我会喜欢的:
.
.
>>> company=row['Company Name']
>>> pandas.get_dataframe_from_variable(company)
Empty DataFrame
Columns: ['Date', 'Daily Profit', 'Daily Loss', 'Daily Revenue']
Index: []
[0 rows x 2 columns]
.
.
Any ideas of how I might get this to work would be much appreciated. 我对如何使它起作用的任何想法将不胜感激。
Thanks in advance! 提前致谢!
You can use a dictionary to contain your DataFrames and use strings as the keys. 您可以使用字典来包含您的DataFrame,并使用字符串作为键。
companies = {'company1':pandas.DataFrame(columns=['Date', 'Daily Profit',
'Daily Loss', 'Daily Revenue']),
'company2':pandas.DataFrame(columns=['Date', 'Daily Profit',
'Daily Loss', 'Daily Revenue'])}
company=row['Company Name'] # Get your company name as a string from your summary.
company_details = companies[company] # Returns a DataFrame.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.