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

add values of two columns pandas

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

pandas assign multiple columns at once

df = pd.DataFrame({'col1':[0,1,2], 'col2':[0,1,2], 'col3':[0,1,2]})

df
   col1  col2  col3
0     0     0     0
1     1     1     1
2     2     2     2

df['col1'], df['col2'], df['col3'] = zip(*df.apply(lambda r: (1, 2, 3), axis=1))

df
   col1  col2  col3
0     1     2     3
1     1     2     3
2     1     2     3
Comment

assign multiple columns pandas

import pandas as pd

df = {'col_1': [0, 1, 2, 3],
        'col_2': [4, 5, 6, 7]}
df = pd.DataFrame(df)

df[[ 'column_new_1', 'column_new_2','column_new_3']] = [np.nan, 'dogs',3]  #thought this wo
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

pandas assign multiple columns

In [1069]: df.assign(**{'col_new_1': np.nan, 'col2_new_2': 'dogs', 'col3_new_3': 3})
Out[1069]:
   col_1  col_2 col2_new_2  col3_new_3  col_new_1
0      0      4       dogs           3        NaN
1      1      5       dogs           3        NaN
2      2      6       dogs           3        NaN
3      3      7       dogs           3        NaN
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 :: axes_style seaborn 
Python :: how to make a calculator in python 
Python :: python create empty list 
Python :: is python idle an ide 
Python :: dataframe cut 
Python :: how to reduce the image files size in python 
Python :: django model queries 
Python :: add column python list 
Python :: run flask in background 
Python :: k means clustering python medium 
Python :: python sort dictionary case insensitive 
Python :: print function python 
Python :: add in python 
Python :: python range function examples 
Python :: turtle write function in turtle package python 
Python :: python 3.6 release date 
Python :: getting a column that corresponds to the average of two columns in pandas 
Python :: python iterate over instances of class 
Python :: how to block a ip adress 
Python :: list append string 
Python :: simple plt plot 
Python :: pandas numpy multiplicar dos columnas segun una condicion 
Python :: multiclasshead 
Python :: required depend filed odoo 
Python :: roll a dice 
Shell :: bash watch cpu frequency, linux cpu frequency, linux live cpu frequency 
Shell :: npm cache clean 
Shell :: filename too long git 
Shell :: git name email 
Shell :: update angular cli globally 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =