Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to add the sum of multiple columns into another column in a dataframe

print(df)
   Apples  Bananas  Grapes  Kiwis
0     2.0      3.0     NaN    1.0
1     1.0      3.0     7.0    NaN
2     NaN      NaN     2.0    3.0

df['Fruit Total']=df.iloc[:,-4:].sum(axis=1)

print(df)
   Apples  Bananas  Grapes  Kiwis  Fruit Total
0     2.0      3.0     NaN    1.0          6.0
1     1.0      3.0     7.0    NaN         11.0
2     NaN      NaN     2.0    3.0          5.0
Comment

sum two columns pandas

sum_column = df["col1"] + df["col2"]
Comment

add values of two columns pandas

df['sum'] = df['a'] + df['b']  # sum is a new column
Comment

PREVIOUS NEXT
Code Example
Python :: next() python 
Python :: python package 
Python :: spark list tables in hive 
Python :: pyautogui tab key 
Python :: python try except continue loop 
Python :: how to convert binary to integer in python 
Python :: python rps 
Python :: add to a list python 
Python :: Adding labels to histogram bars in matplotlib 
Python :: extract a column from a dataframe in python 
Python :: regex name extract 
Python :: how to check if python is installed 
Python :: remove prefix in python 3.6 
Python :: python random walk 
Python :: django filter queryset by date 
Python :: make sns heatmap colorbar larger 
Python :: python string trim 
Python :: np.where 
Python :: semicolon in python 
Python :: python split string after substring 
Python :: time difference between two datetime.time 
Python :: how to add to the end of an array python 
Python :: print whole list python 
Python :: python train test val split 
Python :: python calculator file size to megabytes 
Python :: beautifulsoup find 
Python :: numpy.sign() in Python 
Python :: python string cut 
Python :: Python "for in" loop to print the last item in the list 
Python :: python plot horizontal line 
ADD CONTENT
Topic
Content
Source link
Name
5+7 =