Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looping through rows of pandas when the equation also changes

Tags:

python

pandas

  • I need to ignore timestamps and loop through rows this way.
import pandas as pd
import numpy as np

time = ['11:50', '12:50', '13:50']
data_1 = {'time': time,
          'n1': [1, 5, 8],
          'n2': [2, 6 ,7],
          'n3': [3, 7 ,6],
          'n4': [4, 8, 5],
        }

df1 = pd.DataFrame(data = data_1)
df1

I am trying to multiply:

  • row 1 * (10^0)
  • row 2 * (10^1)
  • row 3 * (10^2)
  • ...
  • row n * (10^(n-1))

Before:

time n1 n2 n3 n4
0 11:50 1 2 3 4
1 12:50 5 6 7 8
2 13:50 8 7 6 5

Expected result:

time n1 n2 n3 n4
0 11:50 1 2 3 4
1 12:50 50 60 70 80
2 13:50 800 700 600 500
like image 381
n3a5p7s9t1e3r Avatar asked Sep 11 '25 08:09

n3a5p7s9t1e3r


1 Answers

You can use mul on index axis:

df1.iloc[:, 1:] = df1.iloc[:, 1:].mul(10**df1.index, axis=0)
print(df1)

# Output
    time   n1   n2   n3   n4
0  11:50    1    2    3    4
1  12:50   50   60   70   80
2  13:50  800  700  600  500

You can replace df1.index by np.arange(len(df1)) if your index is not a RangeIndex.

like image 81
Corralien Avatar answered Sep 13 '25 22:09

Corralien