Perform a conditional operation on a pandas column

2024/10/4 5:31:01

I know that this should be simple, but I want to take a column from a pandas dataframe, and for only the entries which meet some condition (say less than 1), multiply by a scalar (say 2).

For example, in this dataframe,

df = pd.DataFrame(randn(5,4),index='A B C D E'.split(),columns='W X Y Z'.split())W         X       Y        Z  A    2.706850 0.628133 0.907969 0.503826 
B    0.651118 -0.319318 -0.848077 0.605965 
C    -2.018168 0.740122 0.528813 -0.589001 
D    0.188695 -0.758872 -0.933237 0.955057 
E    0.190794 1.978757 2.605967 0.683509 

if I'm interested in carrying out this operation on column W, the result should be

          W         X       Y        Z  A    2.706850 0.628133 0.907969 0.503826 
B    1.302236 -0.319318 -0.848077 0.605965 
C    -4.036336 0.740122 0.528813 -0.589001 
D    0.37739 -0.758872 -0.933237 0.955057 
E    0.381588 1.978757 2.605967 0.683509

I have the below for an absolute assignment:

df.loc[df['W'] < 1, 'W'] = 4

but I'm not sure how to use the actual values from W.

Thanks in advance!

Answer

In your case, just use the *= operator to make your multiplication in place:

If your Original dataframe looks like:

>>> dfW         X         Y         Z
0  2.706850  0.628133  0.907969  0.503826
1  0.651118 -0.319318 -0.848077  0.605965
2 -2.018168  0.740122  0.528813 -0.589001
3  0.188695 -0.758872 -0.933237  0.955057
4  0.190794  1.978757  2.605967  0.683509

You can use:

df.loc[df['W'] < 1, 'W'] *= 2

resulting in this:

>>> dfW         X         Y         Z
0  2.706850  0.628133  0.907969  0.503826
1  1.302236 -0.319318 -0.848077  0.605965
2 -4.036336  0.740122  0.528813 -0.589001
3  0.377390 -0.758872 -0.933237  0.955057
4  0.381588  1.978757  2.605967  0.683509

This is equivalent to the following:

df.loc[df['W'] < 1, 'W'] = df.loc[df['W'] < 1, 'W'] * 2
https://en.xdnf.cn/q/70647.html

Related Q&A

How to programmatically get SVN revision number?

Like this question, but without the need to actually query the SVN server. This is a web-based project, so I figure Ill just use the repository as the public view (unless someone can advise me why this…

Convert fractional years to a real date in Python

How do I convert fractional years to a real date by using Python? E. g. I have an array [2012.343, 2012.444, 2012.509] containing fractional years and I would like to get "yyyy-mm-dd hh:mm".

Django template: Translate include with variable

I have a template in which you can pass a text variable. I want to include this template into another one but with a translated text as its variable. How can you achieve this?I would like something li…

Pandas - Creating a New Column

I have always made new columns in pandas using the following:df[new_column] = valueI am using this method, however, am receiving the warning for setting a copy.What is the way to make a new column with…

Adding an extra column to (big) SQLite database from Pandas dataframe

I feel like Im overlooking something really simple, but I cant make it work. Im using SQLite now, but a solution in SQLAlchemy would also be very helpful.Lets create our original dataset:### This is ju…

error inserting values to db with psycopg2 module [duplicate]

This question already has answers here:psycopg2: cant adapt type numpy.int64(4 answers)Inserting records into postgreSQL database in Python(3 answers)Closed 3 months ago.I am attempting to insert a dat…

NaN values in pivot_table index causes loss of data

Here is a simple DataFrame:> df = pd.DataFrame({a: [a1, a2, a3],b: [optional1, None, optional3],c: [c1, c2, c3],d: [1, 2, 3]}) > dfa b c d 0 a1 optional1 c1 1 1 a2 None c2…

ModuleNotFoundError in Docker

I have imported my entire project into docker, and I am getting a ModuleNotFoundErrorfrom one of the modules I have created.FROM python:3.8 WORKDIR /workspace/ COPY . /workspace/ RUN pip install pipenv…

Can I use md5 authentication with psycopg2?

After two hours of reading documentation, source code and help-threads, Im giving up. I cant get psycopg2 to authenticate with a md5-string. According to this thread I dont have to anything besides ena…

Python checking __init__ parameter

Ive been trying to figuring this out for the last few hours, and Im about to give up.How do you make sure that in python only a matching specific criteria will create the object?For example, lets say …