Home > Software engineering >  mathematical process in Dataframe
mathematical process in Dataframe

Time:08-30

import numpy as np
import pandas as pd

data = np.random.randint(0,10,size=(10000,5))
df = pd.DataFrame(data, columns=['A', 'B', 'C','D','Output'])

df.head()

I will create dataframe

  if a** 4 b** 3 c**2 d %2 == 0:
    return 0:  (output=0)
  else:
    return 1:   (output=1)

how can ı write output values

CodePudding user response:

Assuming you intend for the % operation to apply to (a**4 b**3 c**2 d), you can update the Output column of the dataframe like this:

df['Output'] = (((df.A**4   df.B**3   df.C**2   df.D) %2) != 0).astype(int)

Output of df.head():

   A  B  C  D  Output
0  9  8  4  9       0
1  6  5  9  5       1
2  3  6  1  3       1
3  8  3  3  0       0
4  8  3  5  7       1
  • Related