user1801060 user1801060 - 1 year ago 37
Python Question

Write specific data in one cell depending on contents of another cell in Pandas

I am a Pandas newbie and I have a csv file with about 50 different columns. Some of them contain a "-1" value and the last column I have named "Holder" to store the results of some comparisons I make on the other columns.

Data is of the form

Row 1:
Investments_Cash Holder
0 NaN

Row 2:
Investments_Cash Holder
0 NaN

Row 3:
Investments_Cash Holder
-1 NaN

For the rows that contain "Investments_Cash" of -1, I want to set the corresponding "Holder" column value to "Found". How can I do this?

I've tried

if df.Investments_Cash == -1:
df.Holder = "Found"

but I get an error
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Answer Source
df.loc[df.Investments_Cash == -1, 'Holder'] = 'Found'