Home > Blockchain >  new column(s) in a pandas dataframe based on values calculated from rest of the columns
new column(s) in a pandas dataframe based on values calculated from rest of the columns

Time:10-24

I am struggling with figuring out how to return a conditional value to a column based on values in selected columns on same index row. see the attached picture.

  1. I have a df where "<0" column is supposed to count the number of instances in previous 18 columns where valueas are less than 0.

  2. I also need to count the total number of columns excluding NaN for each row.

any suggestions?

enter image description here

CodePudding user response:

import pandas as pd

data = {
    "1": [420, -3, 390],
    "2": [50, None, 45],
    "3": [-2,None,4]
}

#load data into a DataFrame object:
df = pd.DataFrame(data)

for i in range(len(df.columns)):
    df.loc[i,"Na_Counter"] = df.iloc[i].isnull().sum()
    df.loc[i,"Negative_Counter"] = (df.iloc[i] < 0).sum().sum()

i hope it works

CodePudding user response:

You can use:

s1 = df.lt(0).sum(axis=1)
s2 = df.notna().sum(axis=1)

df['<0'] = s1
df['TotCount (ex Nan)'] = s2

Or:

cols = df.columns

df['<0'] = df[cols].lt(0).sum(axis=1)
df['TotCount (ex Nan)'] = df[cols].notna().sum(axis=1)
  • Related