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

add values from 2 columns to one pandas

df['Fruit Total']= df.iloc[:, -4:-1].sum(axis=1)
print (df)
   Apples  Bananas  Grapes  Kiwis  Fruit Total
0     2.0      3.0     NaN    1.0          5.0
1     1.0      3.0     7.0    NaN         11.0
2     NaN      NaN     2.0    3.0          2.0
Comment

insert multiple column pandas

df['column_new_1'], df['column_new_2'], df['column_new_3'] = [np.nan, 'dogs', 3]
Comment

PREVIOUS NEXT
Code Example
Python :: split and only grab first part of string 
Python :: python webbrowser module 
Python :: how to detect the reaction to a message discord.py 
Python :: how to combine strings python 
Python :: python print all variables in memory 
Python :: plt.hist using bins 
Python :: polish notation python 
Python :: get url param in get django rest 
Python :: sqlite operational error no such column 
Python :: Could not find a version that satisfies the requirement ckeditor 
Python :: plotly pie chart in pie chart 
Python :: python community 
Python :: np reshape 
Python :: make virtual environment wrapper python 3 
Python :: pytorch version python command 
Python :: argmax implementation 
Python :: arithmetic in python 
Python :: serialization in django 
Python :: How to count a specific number in a python list? 
Python :: radians in python turtle 
Python :: python replace with something else 
Python :: read ms word with python 
Python :: how to use dictionary in python 
Python :: skip to next iteration python 
Python :: dicttoxml python? 
Python :: pytest - parameterizing tests 
Python :: slice in python 
Python :: what is a class in python 
Python :: power of array 
Python :: soup.find_all attr 
ADD CONTENT
Topic
Content
Source link
Name
8+7 =