Emin Akdeniz
Emin Akdeniz

Reputation: 3

mathematical process in Dataframe

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

Upvotes: 0

Views: 39

Answers (1)

constantstranger
constantstranger

Reputation: 9379

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

Upvotes: 1

Related Questions