Home > Mobile >  Iterating through columns in a dataframe to add to a Total Value column in Python pandas
Iterating through columns in a dataframe to add to a Total Value column in Python pandas

Time:05-12

I am trying to iterate through some column names to add up the total value in a 'TotalValue' column. I get:

KeyError: 'TotalValue'

I have shown what the equation would look like if I typed it out to get the total.

df['TotalValueByHand'] = df['Value_SCHD']   df['Value_VBR']   df['Value_IXUS']   df['Value_MDIZX']

I would like to iterate through columns though because ultimately I plan to have a lot more in the list. Sorry if the indentation comes out weird, it looked okay when I typed it.

stockList = ["SCHD", "VBR", "MDIZX", "IXUS"]

for s in stockList:
  
    df['TotalValue'] = np.where(df['Value_'   s] > 0, df['TotalValue']   df['Value_'   s], df['TotalValue'])

CodePudding user response:

If need sum only values greater like 0 use list comprehension for generate all columns names with DataFrame.clip and sum:

stockList = ["SCHD", "VBR", "MDIZX", "IXUS"]

cols = [f'Value_{s}' for s in stockList]
df['TotalValue'] = df[cols].clip(lower=0).sum(axis=1)

If need only sum:

cols = [f'Value_{s}' for s in stockList]
df['TotalValue'] = df[cols].sum(axis=1)

Your solution is possible if create first column filled by 0:

df['TotalValue'] = 0

for s in stockList:

    df['TotalValue'] = np.where(df['Value_'   s] > 0, df['TotalValue']   df['Value_'   s], df['TotalValue'])
  • Related