Change a pandas DataFrame column value based on another column value

2024/11/16 17:31:11

I have a dataframe with two columns each of which represents an organism. They are called ORG1 and ORG2 I want to move the values of ORG2 into ORG1 for the corresponding index value.

So, if ORG1 is 'A' and ORG2 is 'B' I want ORG1 to take the value 'B' from ORG2.

I have already started work to identify indexes of the ORG2 organisms that I want to move, as follows:

def move_org2(x):org2_matches = Series(x.ORG2.str.count("ESBL"))return x.ix[org2_matches == 1]org2_DF = move_org2(DF)org2_DF.ORG2.index

What is the best way to use this to change ORG1 values with the values at corresponding ORG2 indices

Answer
In [13]: df
Out[13]:ORG1  ORG2
0    A  ESBL
1    B     P
2    C     Q
3    D     R
4    E  ESBLIn [14]: cond = df.ORG2 == 'ESBL'In [15]: df.ORG1[cond] = df.ORG2[cond]In [16]: df
Out[16]:ORG1  ORG2
0  ESBL  ESBL
1     B     P
2     C     Q
3     D     R
4  ESBL  ESBL
https://en.xdnf.cn/q/71317.html

Related Q&A

How to lock a sqlite3 database in Python?

Is there a way to explicitly acquire a lock on a sqlite3 database in Python?

How to install pyzmq on an Alpine Linux container?

I have a container with the python:3.6-alpine kernel. I have a problem installing the pyzmq via pip on this: Dockerfile: FROM python:3.6-alpineRUN mkdir /code RUN apk add vim WORKDIR / ADD . /codedocke…

How to Get Value Out from the Tkinter Slider (Scale)?

So, here is the code I have, and as I run it, the value of the slider bar appears above the slider, I wonder is there a way to get that value out? Maybe let a=that value. ;)from Tkinter import *contr…

Python cassandra-driver OperationTimeOut on every query in Celery task

I have a problem with every insert query (little query) which is executed in celery tasks asynchronously. In sync mode when i do insert all done great, but when it executed in apply_async() i get this:…

Function which returns the least-squares solution to a linear matrix equation

I have been trying to rewrite the code from Python to Swift but Im stuck on the function which should return the least-squares solution to a linear matrix equation. Does anyone know a library written i…

Divide .csv file into chunks with Python

I have a large .csv file that is well over 300 gb. I would like to chunk it into smaller files of 100,000,000 rows each (each row has approximately 55-60 bytes).I wrote the following code:import panda…

Why cant I use operator.itemgetter in a multiprocessing.Pool?

The following program:import multiprocessing,operator f = operator.itemgetter(0) # def f(*a): return operator.itemgetter(0)(*a) if __name__ == __main__:multiprocessing.Pool(1).map(f, ["ab"])f…

Writing a compiler for a DSL in python

I am writing a game in python and have decided to create a DSL for the map data files. I know I could write my own parser with regex, but I am wondering if there are existing python tools which can do …

How to setup Celery to talk ssl to Azure Redis Instance

Using the great answer to "How to configure celery-redis in django project on microsoft azure?", I can configure Celery to use Azure Redis Cache using the non-ssl port, 6379, using the follo…

Cant save data from yfinance into a CSV file

I found library that allows me to get data from yahoo finance very efficiently. Its a wonderful library.The problem is, I cant save the data into a csv file.Ive tried converting the data to a Panda Da…