简体   繁体   English

如何使用变量值调用数据框?

[英]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.

相关问题 我如何使用一个变量的值在python中调用另一个变量? - How can i use the value of one variable to call another in python? 如何存储 DataFrame 的变量并使用它发送 email? - How can i store a variable of a DataFrame and use it to send a email? 如何使用输入函数调用变量的值? - How do I use the input function to call the value of a variable? 我可以使用 for 循环将 dataframe 与变量连接起来吗? - Can i use a for loop to concat dataframe with variable? 如何使用 dataframe 中的值来查找属性 - How can I use a value in a dataframe to look up an attribute 如何在 dataframe 中使用计数列表值 - How can I use count list value in dataframe 如何使用 dataframe 的 column_name 作为行上的值? - How can I use the column_name of a dataframe as a value on the rows? 在 pandas DataFrame 中,如何将列中的特定值存储到变量中,然后从列中删除该值? - In pandas DataFrame, how can I store a specific value from a column into a variable, and then subsequently remove that value from the column? 如何在 function 定义中使用变量的值作为关键字? - How can I use the value of a variable as a keyword in a function definition? 如何使用Python格式字符串中的变量的值 - How can I use the value of a variable in Python format string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM