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 :: python if condition assignment in one line 
Python :: save numpy array 
Python :: generate secret key python 
Python :: start project django 
Python :: pyspark dataframe to parquet 
Python :: if statement in one-line for loop python 
Python :: how to show a frequency distribution based on date in python 
Python :: pandas cumulative mean 
Python :: deep copy a dataframe 
Python :: pretty size python 
Python :: pythone csv 
Python :: subtract number from each element in list python 
Python :: pd.read_csv 
Python :: add two numbers in python 
Python :: pandas convert numbers in parentheses to negative 
Python :: Cast image to float32 
Python :: python while true loop 
Python :: dir() in python 
Python :: how to use setattr Python 
Python :: Creating a donut plot python 
Python :: assert python 
Python :: Extract bounding boxes OpenCV 
Python :: how to print upto 5 decimal places in python 
Python :: get last 3 things in a list python 
Python :: pandas create a new column based on condition of two columns 
Python :: python get desktop environment 
Python :: pandas count values by column 
Python :: prevent division by zero numpy 
Python :: django createssuperuser 
Python :: planets code 
ADD CONTENT
Topic
Content
Source link
Name
2+2 =