简体   繁体   中英

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

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'])

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'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM