简体   繁体   中英

iterating though and adding columns pandas

I was working with a practice data set and was told to make a new column with the sum of others like this:

df["new column"] = df["column4"] + df["column5"] + df["column6"] + df["column7"] + etc....

I feel this is inefficient due to the fact that they are even line up (columns 4-9) and I need to speed up my coding (data sci competition coming up!).

how may i do this in a shorter / more efficient way!

I've tried using variations of stuff like df['Total'] = sum(df.columns[4:9]) but its not working out

disclaimer: i am new to pandas

Assuming these columns are next to each other, use column slicing:

df['new column'] = df.loc[:, 'column4':'column9'].sum(axis=1)

axis=1 mean you are sum across the columns, so you are calculating a total per row.


If they are separated, you can get their sum with a list:

cols = ['column4', 'column5', 'column6', 'column7', 'column8', 'column9']
df['new column'] = df[cols].sum(axis=1)

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