0

I'm quite new to coding and working on a project where I am modifying a few dataframes.

Now I have a dataframe containing a few a columns where one column called Check has a value of either True or False. This value is given based on a formula that is also in the code

What I want to achieve is that another column called Price will add + 1 until column Check is True

I have tried the following lines of code of none of them worked.

df['Price'] = df['Price'].apply(lambda x: +1 if x == False)

i = df['Check']
while i == False:
    df = df['Price'] =+ 1

df['PRICE'] = df.ix[df.Check.isin(False), 'PRICE'] = +1

I hope someone can help me out

Alex
  • 6,610
  • 3
  • 20
  • 38
Luuk Botzen
  • 5
  • 1
  • 5
  • Welcome! See [here](https://stackoverflow.com/a/20159305/3279716) for tips on how to format your question so that we can reproduce what you're doing. – Alex Aug 08 '18 at 17:52
  • Also, straight off the bat, I can see that you've used `PRICE` and `Price`. If you provide a snippet of your dataframe and some example output then we can help. – Alex Aug 08 '18 at 17:54
  • also, be careful `a = +1` is the same as `a =+ 1` and `a = 1`. Only `a += 1` increments the value by one i..e. it is a short for `a = a + 1` – xdze2 Aug 08 '18 at 20:03

1 Answers1

1

First, an example dataframe:

import pandas as pd

data = {'check': [True, True, False], 'price': [3, 2, 0]}
df = pd.DataFrame(data)
df

The example dataframe is:

    check   price
0   True    3
1   True    2
2   False   0

Using the method DataFrame.loc, the data we want to change can be selected. The rows are selected based on the boolean array df['check'] == True, and the column is select using its name price.

df.loc[df['check'] == True, 'price']

gives, the indexes and the price values for the selected rows:

0   3
1   2

Then, the price in the rows with check true can be incremented:

df.loc[df['check'] == True, 'price'] += 1
df

now df is:

    check   price
0   True    4
1   True    3
2   False   0

By inserting this line into a loop, the prices will be continuously incremented. However, something is needed to switch at some point the value of check to false.

Note that == True is redundant: df.loc[df['check'], 'price'] += 1 works too

Example with a while loop:

while not df['check'].all():   # loop while at least one check value is false
    print(df, '\n')
    df.loc[df['check'], 'price'] += 1  # increment the 'checked' price py one
    df.loc[df['check'] == False, 'check'] = True   # Switch the false to true, <- insert your formula
xdze2
  • 3,986
  • 2
  • 12
  • 29
  • Thanks for the help! Really useful. I added your code. But using a while loop gives me this error : `ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().` I now have this code: `i = df['check'] while i == False: df.loc[df['check'] == False, 'price'] += 1` and then my formula – Luuk Botzen Aug 08 '18 at 19:28
  • For what the while loop is for: Is it to go through all the rows? (then it is not needed) or is it to increment the prices many times in time? – xdze2 Aug 08 '18 at 19:58
  • It's to increment the price until the all rows of check are True. – Luuk Botzen Aug 08 '18 at 19:59
  • I got it working, thanks a lot for your help! You're the best :D – Luuk Botzen Aug 08 '18 at 20:19
  • I added another example with a while loop – xdze2 Aug 08 '18 at 20:39