0

I have a data frame (below is only the subset):

Data1 Data2 Status
1 False
2 True
5 True

I would like to put a value(string) in column Data2 = "Done" if the Status = True (boolean).

My code is:

d = {"Data1":[1,2,5], "Data2":["","",""], "Status": [False, True, True]}
df = pd.DataFrame(data = d)


for x in range(len(df["Status"])):
     if df["Status"].loc[x] == True:
        df["Data2"].loc[x] = "Done"
     else:
        df["Data2"].loc[x] = ""

This code works. My question is: is there any way to do it without loop?

teteh May
  • 455
  • 2
  • 11

3 Answers3

3

You can use Numpy (just one line of code):

import numpy as np
df['Data2'] = np.where(df['Status'], 'Done', '')

The syntax is:

np.where(<criteria>, <value if criteria met>, <value if criteria not met>)
gtomer
  • 5,643
  • 1
  • 10
  • 21
0

Sure there is. Pandas indexing can use an array of booleans to select items.

df.loc[df["Status"]==True,"Data2"] = "Done"
df.loc[df["Status"]==False,"Data2"] = ""
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
-1
import pandas as pd

d = {"Data1": [1, 2, 5], 
     "Data2": ["", "", ""], 
     "Status": [False, True, True]} df = pd.DataFrame(data=d)

def update_data2(row):
    return "Done" if row["Status"] else ""

df["Data2"] = df.apply(update_data2, axis=1)
nuggetier
  • 172
  • 2
  • 6