How to name dataframe using a list of inputs
I have many wells, every well belongs to some branch and then a few branches together creates a field. I use cycle to do the same operation for every well
items=['1','5','8','22','2']
for item in items
well=item
......
This cycle loads data from database, cleans it, calculates for each well rates, pressures etc. This cycle also creates plots and writes output data results to excel file which contains sheets with names 1,5,8,22,2. But then I have to summ rates, pressures etc. for branches, and then to summ branches to field.
Is there a way to name a resulting dataframe using this item well name list too?
Because currenlty for branches I have write by hands:
well1=pd.read_excel('results.xlsl ', sheet_name='1')
well5=pd.read_excel('results.xlsl ', sheet_name='1')
branch['rate']=well1['rate']+well5['rate']
etc.
Instead of creating separate DataFrame variables for each well (like well1, well5, etc.), you can store them in a dictionary where each key is the well name (or number) and the value is the corresponding DataFrame. This way, you can easily access and manipulate data for each well using its name as a key.
Example in py:
import pandas as pd
# Assuming 'items' contains the names of your wells
items = ['1', '5', '8', '22', '2']
# Dictionary to hold the DataFrame for each well
wells_data = {}
for item in items:
# Load data from Excel, clean it, calculate rates, pressures, etc.
wells_data[item] = pd.read_excel('results.xlsx', sheet_name=item)
# Now, wells_data['1'] is the DataFrame for well 1, wells_data['5'] is for well 5, and so on.
Once you have your well DataFrames in a dictionary, you can aggregate this data for branches and fields.
Example of aggregating data for branch:
# Define which wells belong to each branch
# This is just an example; adjust according to your actual branch-well relationships
branch_wells = {
'branch1': ['1', '5'],
'branch2': ['8', '22', '2']
}
# Dictionary to hold the aggregated data for each branch
branches_data = {}
for branch, wells in branch_wells.items():
# Initialize an empty DataFrame for the branch or start with the first well's data
branch_data = pd.DataFrame()
for well in wells:
if branch_data.empty:
branch_data = wells_data[well].copy()
else:
# Assuming you want to sum the rates, pressures, etc.
# Adjust the calculation as needed for your specific metrics
branch_data['rate'] += wells_data[well]['rate']
# Repeat for other metrics as needed
branches_data[branch] = branch_data
# Now, branches_data['branch1'] is the aggregated DataFrame for branch1, etc.
This way You'll define which wells belong to which branches, and then perform your calculations (e.g., summing rates, pressures) by accessing the relevant DataFrames from your dictionary.