Python-Pandas While loop

2024/11/13 4:16:56

I am having some trouble with the DataFrame and while loop:

A     B
5     10
5     10
10    5

I am trying to have a while loop that:

while (Column A < Column B):Column A = Column A + (Column B / 2)Column B = Column B / 2

Any ideas how to do this.

Answer

You can vectorise this calculation to avoid explicit loops:

mask = df['A'] < df['B']df.loc[mask, 'A'] = df['A'] + df['B'] / 2
df.loc[mask, 'B'] = df['B'] / 2print(df)#       A    B
# 0  10.0  5.0
# 1  10.0  5.0
# 2  10.0  5.0
https://en.xdnf.cn/q/120509.html

Related Q&A

split an enumerated text list into multiple columns

I have a dataframe column which is a an enumerated list of items and Im trying to split them into multiple columns. for example dataframe column that looks like this:ID ITEMSID_1 1. Fruit 12 oranges 2…

Python using self as an argument [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable…

how to shuffle questions in python

please anyone help me i have posted my whole work down can anyone tell me how to shuffle theses five questions it please i will be very thankful.print("welcome to the quiz") Validation = Fals…

Selenium python do test every 10 seconds

I am using selenium (python) testing and I need to test my application automatically every 10 seconds.How can I do this?

Type Object has no attribute

I am working on a program, but I am getting the error "Type object Card has no attribute fileName. Ive looked for answers to this, but none that Ive seen is in a similar case to this.class Card: R…

Generate random non repeating samples from an array of numbers

I made a battleships game and I now need to make sure that the computer doesnt attack at the same spot twice.My idea of it is storing each shots co-ordinates in a variable which gets added to whenever …

How to get back fo first element in list after reaching the end? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 5 years ago.Improve…

Collision detection on the y-axis does not work (pygame)

I am trying to get the collision detection in my code to work. I am using vectors and I want the player sprite to collide and stop when it collides with a sprite group called walls. The problem is that…

Search for string within files of a directory

I need help writing a light weight Python (v3.6.4) script to search for a single keyword within a directory of files and folders. Currently, I am using Notepad++ to search the directory of files, altho…

Extract parent and child node from python tree

I am using nltks Tree data structure.Below is the sample nltk.Tree.(S(S(ADVP (RB recently))(NP (NN someone))(VP(VBD mentioned)(NP (DT the) (NN word) (NN malaria))(PP (TO to) (NP (PRP me)))))(, ,)(CC an…