OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Pandas expand Dataframe to calculate Fractions

  • Thread starter Thread starter Fscir
  • Start date Start date
F

Fscir

Guest
Currently im working on a problem where i have to calculate fractions on a dataset. The Dataframe is roughly in this form

LocationTotal ValuesTotal Values 2
Location 1100300
Location 2200400

Then i have some fractions i want to calculate:

IdentFraction
Frac A0.25
Frac B0.75

I would like to create a new dataframe like this

LocationTotal ValuesTotal Values 2
Location 1 A2575
Location 1 B75225
Location 2 A50100
Location 2 B150300

Right now im iterating over the rows, creating a new row based on the source row, appending the newly created row to list and then merging the list with the original dataframe

Code:
ccalculated = []
for _, row in df_source.iterrows():
   for ident in df_fractions:
      new_row = row.copy()
      scaling_factor = df_fractions[df_fractions.loc[:, "Ident"] == "Frac A"]
      for values in ValueCols: 
         new_row[values] = np.multiply(new_row["Total Values"], scaling_factor)
     calculated.append(new_row)

But this is very slow and i feel like there has to be a better solution
<p>Currently im working on a problem where i have to calculate fractions on a dataset.
The Dataframe is roughly in this form</p>
<div class="s-table-container"><table class="s-table">
<thead>
<tr>
<th>Location</th>
<th>Total Values</th>
<th>Total Values 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>Location 1</td>
<td>100</td>
<td>300</td>
</tr>
<tr>
<td>Location 2</td>
<td>200</td>
<td>400</td>
</tr>
</tbody>
</table></div>
<p>Then i have some fractions i want to calculate:</p>
<div class="s-table-container"><table class="s-table">
<thead>
<tr>
<th>Ident</th>
<th>Fraction</th>
</tr>
</thead>
<tbody>
<tr>
<td>Frac A</td>
<td>0.25</td>
</tr>
<tr>
<td>Frac B</td>
<td>0.75</td>
</tr>
</tbody>
</table></div>
<p>I would like to create a new dataframe like this</p>
<div class="s-table-container"><table class="s-table">
<thead>
<tr>
<th>Location</th>
<th>Total Values</th>
<th>Total Values 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>Location 1 A</td>
<td>25</td>
<td>75</td>
</tr>
<tr>
<td>Location 1 B</td>
<td>75</td>
<td>225</td>
</tr>
<tr>
<td>Location 2 A</td>
<td>50</td>
<td>100</td>
</tr>
<tr>
<td>Location 2 B</td>
<td>150</td>
<td>300</td>
</tr>
</tbody>
</table></div>
<p>Right now im iterating over the rows, creating a new row based on the source row, appending the newly created row to list and then merging the list with the original dataframe</p>
<pre><code>ccalculated = []
for _, row in df_source.iterrows():
for ident in df_fractions:
new_row = row.copy()
scaling_factor = df_fractions[df_fractions.loc[:, "Ident"] == "Frac A"]
for values in ValueCols:
new_row[values] = np.multiply(new_row["Total Values"], scaling_factor)
calculated.append(new_row)
</code></pre>
<p>But this is very slow and i feel like there has to be a better solution</p>
 

Latest posts

I
Replies
0
Views
1
impact christian
I
Top