Khris
Khris

Reputation: 3212

Pandas: How to properly set value defined by boolean index with value from another cell defined by boolean index?

I want to choose a value in my dataframe using boolean indexing and fill it with another value from the same dataqframe also choosen with boolean indexing.

Now the problem is of course, that I can't expect any of them to return exactly one value, and even if I can the syntax can't.

So right now I have to do something like this:

import pandas as pd
import numpy as np
L1 = ['A','A','A','A','B','B','B','B']
L2 = ['a','b','c','d','a','b','c','d']
L3 = [1,2,3,4,5,6,7,8]
df = pd.DataFrame({"L1":L1,"L2":L2,"L3":L3})

filtered = df.loc[(df.L1 == 'B')&(df.L2 == 'c'),"L3"]
if not filtered.empty:
    value = filtered.values[0]
else:
    value = np.nan
df.loc[(df.L1 == 'A')&(df.L2 == 'c'),"L3"] = value

First I have to choose the target value which returns a series, then have to make sure that at least one value is in there and only then I can set it.

If I weren't sure that there was only one value I'd have to do consider the case of several value being returned as well and how to deal with that case.

Is there a shorter more elegant way to do this, basically a function that says "fill value where conditions XYZ are met with value where conditions ABC are met" that can be provided with a function to handle multiple values, e.g. mean or first?

Upvotes: 1

Views: 190

Answers (1)

jezrael
jezrael

Reputation: 862691

I think you can use Series.item if need filter only one value but unfortuntely with if else:

value = np.nan if filtered.empty else filtered.item()

Upvotes: 1

Related Questions