python - How to name dataframes dynamically in Python?
问题描述
I have an excel file which contains more than 30 sheets. However the operation that I do on each sheet remains the same more or less. But my objective is to create a separate dataframe for each sheet, so that I can refer in the future
This is what I tried but it throws an error
xls = pd.ExcelFile('DC_Measurement.xlsx')
sheets = xls.sheet_names
for s in sheets:
print(s)
'df '+ s = pd.read_excel(xls, sheet_name=s)
So, it's like I want 30 dataframes to be created and each dataframe will have the sheet name as the suffix name. I tried using the "+" operator but it didn't help either. It threw an error message as shown below
SyntaxError: can't assign to operator
How can I create dataframes on the fly and name them ?
解决方案
You could use something like this:
for s in sheets:
vars()['df'+ s] = pd.read_excel(xls, sheet_name=s)
推荐阅读
- java - 由 java.lang.NumberFormatException 引起:无效的浮点数:“∞”
- sql-server - 在 CASE 语句期间 SQL 检查空值
- sql-server - 循环遍历 SQL Server 中的游标
- powershell - 以编程方式在 Visual Studio 中注册 TFS 服务器?
- c# - 抽象类中的 EF6 OrderBy 泛型表达式
- java - 如何在 Android 的 Fragment 中使用 Volley?
- php - Laravel:在数千行上使用“pluck”会导致 500 错误
- python-3.x - Keras自定义层如何有多个返回
- c++ - 升压进程运行 su
- filter - 过滤 Elastic Beanstalk SNS 主题