Member-only story

Python — How to update multiple columns at once in dataframe?

Ryan Arjun
4 min readNov 19, 2020

--

Working as Python developer, data analysts or data scientists for any organisation then it is very important for you to know how to play with Dataframes. We understand, we can add a column to a dataframe and update its values to the values returned from a function or other dataframe column’s values as given below -

# pandas library for data manipulation in python
import pandas as pd
# create a dataframe with number values
df = pd.DataFrame({'Num':[5,10,15,17,22,25,28,32,36,40,50,]})
#display values from dataframe
df
#create square() function to return single value
#passing variable is x
#return single value
def square(x):
return x*x
#Add new column and update value in it
df['Square of Num'] = [square(i) for i in df['Num']]
#display values from dataframe
df

Problem — The actual function is returning two items, and I want to put these two items in two different new columns.

How to update multiple columns in Dataframe? If you want to update multiple columns in dataframe then you should make sure that these columns must be present in your dataframe. In case, updated columns are not in your dataframe, you have to create them as given…

--

--

Ryan Arjun
Ryan Arjun

Written by Ryan Arjun

BI Specialist || Azure || AWS || GCP — SQL|Python|PySpark — Talend, Alteryx, SSIS — PowerBI, Tableau, SSRS

No responses yet