Monday, March 11, 2024

Conditional Operation on DataFrame

 import pandas as pd

import numpy as np
from numpy.random import randn

np.random.seed(121)
# Create Dataframe
df = pd.DataFrame(randn(5, 4), ['A', 'B', 'C', 'D', 'E'], ['W', 'X', 'Y', 'Z'])
print("DataFrame:\n")
print(df)
print("Check if value > 0")
print(df[df>0])
print("Get all the row where Col. X value is >0")
print(df[df['X']>0])

O/p:
DataFrame:

          W         X         Y         Z
A -0.212033 -0.284929 -0.573898 -0.440310
B -0.330111  1.183695  1.615373  0.367062
C -0.014119  0.629642  1.709641 -1.326987
D  0.401873 -0.191427  1.403826 -1.968769
E -0.790415 -0.732722  0.087744 -0.500286
Check if value > 0
          W         X         Y         Z
A       NaN       NaN       NaN       NaN
B       NaN  1.183695  1.615373  0.367062
C       NaN  0.629642  1.709641       NaN
D  0.401873       NaN  1.403826       NaN
E       NaN       NaN  0.087744       NaN
Get all the row where Col. X value is >0
          W         X         Y         Z
B -0.330111  1.183695  1.615373  0.367062
C -0.014119  0.629642  1.709641 -1.326987

No comments:

Post a Comment