How to creating a new column in python using data from two separate columns of a pandas DataFrame?

Stack Overflow Asked by KDynamic on January 1, 2022

Need help in writing this SQL update statement in python using the pandas library.

UPDATE example_table
WHERE SOME_OTHER_EXISTING_COLUMN IN ('Value1','Value2','Value3','Value4')

Just to clarify, I want to create a NEW_COLUMN which a multiplication of values in the existing_column belonging to example_table SQLtable/dataframe. The values of NEW_COLUMN don’t update if the condition in the where clause is not met. This is important because I would want to multiply with a different number for a another set of values in where condition. Also, it important to know that I am checking the condition on SOME_OTHER_EXISTING_COLUMN in the same SQLtable/dataframe.

I tried the following in python, but somehow it did not work:

value_list = ['Value1','Value2','Value3','Value4']
example_table["NEW_COLUMN"] = pd.DataFrame.where( cond= example_table['SOME_OTHER_EXISTING_COLUMN'].isin(value_list), self= example_table['EXISITNG_COLUMN']*2)

Side note: EXISITNG_COLUMN is float, SOME_OTHER_EXISTING_COLUMN is string/object and NEW_COLUMN should be a float datatype.

One Answer

record = {
    'Age': [21, 19, 20, 18, 17, 21],
    'Stream': ['Math', 'Commerce', 'Science', 'Math', 'Math', 'Science']}

# create a dataframe
dataframe = pd.DataFrame(record, columns=['Age', 'Stream'])

value_list = ['Math','Commerce','Value3','Value4']
dataframe["NEW_COLUMN"] = 2*dataframe['Age'].where(dataframe['Stream'].isin(value_list), 3*dataframe['Age'])


When your column is in your declared list, Age will be multiplied by 2, if not in list, it is multiplied by 3.

Answered by Venkat J on January 1, 2022

Add your own answers!

Related Questions

Truncated backpropagation in PyTorch (code check)

1  Asked on December 7, 2021 by rampatowl


Blurred places in TextField

1  Asked on December 7, 2021


Masonry style menu columns in mega-menu

2  Asked on December 7, 2021 by joe-rr


How to call useState from another Page?

3  Asked on December 7, 2021 by grizzly-bear


Get Base64 encode file-data from Input Form

7  Asked on December 7, 2021 by jordan-baucke


Restart TeamCity server via web interface

2  Asked on December 7, 2021 by krlmlr


SliverOverlapAbsorber being able to accept multiple slivers

1  Asked on December 7, 2021 by basic-coder


Pandas plot – Range

1  Asked on December 7, 2021 by user13770492


Alternate row colours from 2nd row of the table using CSS

5  Asked on December 7, 2021 by raj-kumar-boddu


MsSql Grammar quoteString don’t auto add ‘N’

0  Asked on December 7, 2021 by jay-chu


Why loading a library dynamically makes my app crash?

0  Asked on December 7, 2021 by jpo38


Ask a Question

Get help from others!

© 2023 All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP